【转载】IImagingFactory COM组件托管代码封装

本文介绍了一个.NET图像处理库IImaging的使用方法,包括创建图像、获取图像信息及绘制图像等操作,并提供了一个将PNG图像绘制到PictureBox控件的具体示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class IImaging
    {
        
#region stałe i struktury
        
public enum PixelFormatID : int
        {
            PixelFormatIndexed 
= 0x00010000,
            PixelFormatGDI 
= 0x00020000,
            PixelFormatAlpha 
= 0x00040000,
            PixelFormatPAlpha 
= 0x00080000,
            PixelFormatExtended 
= 0x00100000,
            PixelFormatCanonical 
= 0x00200000,

            PixelFormatUndefined 
= 0,
            PixelFormatDontCare 
= 0,

            PixelFormat1bppIndexed 
= (1 | (1 << 8| PixelFormatIndexed | PixelFormatGDI),
            PixelFormat4bppIndexed 
= (2 | (4 << 8| PixelFormatIndexed | PixelFormatGDI),
            PixelFormat8bppIndexed 
= (3 | (8 << 8| PixelFormatIndexed | PixelFormatGDI),
            PixelFormat16bppRGB555 
= (5 | (16 << 8| PixelFormatGDI),
            PixelFormat16bppRGB565 
= (6 | (16 << 8| PixelFormatGDI),
            PixelFormat16bppARGB1555 
= (7 | (16 << 8| PixelFormatAlpha | PixelFormatGDI),
            PixelFormat24bppRGB 
= (8 | (24 << 8| PixelFormatGDI),
            PixelFormat32bppRGB 
= (9 | (32 << 8| PixelFormatGDI),
            PixelFormat32bppARGB 
= (10 | (32 << 8| PixelFormatAlpha | PixelFormatGDI | PixelFormatCanonical),
            PixelFormat32bppPARGB 
= (11 | (32 << 8| PixelFormatAlpha | PixelFormatPAlpha | PixelFormatGDI),
            PixelFormat48bppRGB 
= (12 | (48 << 8| PixelFormatExtended),
            PixelFormat64bppARGB 
= (13 | (64 << 8| PixelFormatAlpha | PixelFormatCanonical | PixelFormatExtended),
            PixelFormat64bppPARGB 
= (14 | (64 << 8| PixelFormatAlpha | PixelFormatPAlpha | PixelFormatExtended),
            PixelFormatMax 
= 15
        }

        
public enum BufferDisposalFlag : int
        {
            BufferDisposalFlagNone,
            BufferDisposalFlagGlobalFree,
            BufferDisposalFlagCoTaskMemFree,
            BufferDisposalFlagUnmapView
        }

        
public enum InterpolationHint : int
        {
            InterpolationHintDefault,
            InterpolationHintNearestNeighbor,
            InterpolationHintBilinear,
            InterpolationHintAveraging,
            InterpolationHintBicubic
        }

        
public struct BitmapData
        {
            
public uint Width;
            
public uint Height;
            
public int Stride;
            
public PixelFormatID PixelFormat;
            
public IntPtr Scan0;
            
public IntPtr Reserved;
        }

        
public struct ImageInfo
        {
            
public uint GuidPart1;
            
public uint GuidPart2;
            
public uint GuidPart3;
            
public uint GuidPart4;
            
public PixelFormatID pixelFormat;
            
public uint Width;
            
public uint Height;
            
public uint TileWidth;
            
public uint TileHeight;
            
public double Xdpi;
            
public double Ydpi;
            
public uint Flags;
        }

        
#endregion

        
#region interfejsy

        [ComImport, Guid(
"327ABDA7-072B-11D3-9D7B-0000F81EF32E"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        [ComVisible(
true)]
        
public interface IImagingFactory
        {
            
uint CreateImageFromStream(IStream stream, out IImage image);
            
uint CreateImageFromFile(string filename, out IImage image);
            
uint CreateImageFromBuffer([MarshalAs(UnmanagedType.LPArray)] byte[] buffer, uint size, BufferDisposalFlag disposalFlag, out IImage image);
            
uint CreateNewBitmap(uint width, uint height, PixelFormatID pixelFormat, out IBitmapImage bitmap);
            
uint CreateBitmapFromImage(IImage image, uint width, uint height, PixelFormatID pixelFormat, InterpolationHint hints, out IBitmapImage bitmap);
            
uint CreateBitmapFromBuffer(BitmapData bitmapData, out IBitmapImage bitmap);
            
uint CreateImageDecoder();    // pominięte
            uint CreateImageEncoderToStream();    // pominięte
            uint CreateImageEncoderToFile();    // pominięte
            uint GetInstalledDecoders();    // pominięte
            uint GetInstalledEncoders();    // pominięte
            uint InstallImageCodec();    // pominięte
            uint UninstallImageCodec();    // pominięte
        }

        [ComImport, Guid(
"327ABDA9-072B-11D3-9D7B-0000F81EF32E"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        [ComVisible(
true)]
        
public interface IImage
        {
            
uint GetPhysicalDimension(out Size size);
            
uint GetImageInfo(out ImageInfo info);
            
uint SetImageFlags(uint flags);
            
uint Draw(IntPtr hdc, ref Rectangle dstRect, IntPtr srcRect);
            
uint PushIntoSink();    // pominięte
            uint GetThumbnail(uint thumbWidth, uint thumbHeight, out IImage thumbImage);
        }

        [ComImport, Guid(
"327ABDAA-072B-11D3-9D7B-0000F81EF32E"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        [ComVisible(
true)]
        
public interface IBitmapImage
        {
            
uint GetSize(out Size size);
            
uint GetPixelFormatID(out PixelFormatID pixelFormat);
            
uint LockBits(ref Rectangle rect, uint flags, PixelFormatID pixelFormat, out BitmapData lockedBitmapData);
            
uint UnlockBits(ref BitmapData lockedBitmapData);
            
uint GetPalette();    // pominięte
            uint SetPalette();    // pominięte
        }

        [ComImport, Guid(
"0000000c-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        [ComVisible(
true)]
        
public interface IStream
        {
            
void Read([Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] byte[] pv, int cb, IntPtr pcbRead);
            
void Write([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] byte[] pv, int cb, IntPtr pcbWritten);
            
void Seek(long dlibMove, int dwOrigin, IntPtr plibNewPosition);
            
void SetSize(long libNewSize);
            
void CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten);
            
void Commit(int grfCommitFlags);
            
void Revert();
            
void LockRegion(long libOffset, long cb, int dwLockType);
            
void UnlockRegion(long libOffset, long cb, int dwLockType);
            
void Stat();    // pominięte
            void Clone(out IStream ppstm);
        }

        
#endregion

        
#region metody

        
public static IImagingFactory CreateFactory()
        {
            
return (IImagingFactory)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("327ABDA8-072B-11D3-9D7B-0000F81EF32E")));
        }

        
#endregion
    }

To check that You
're using it well I'm also providing an example of how to paint a PNG onto a pictureBox control during its OnPaint event:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    Graphics g 
= e.Graphics;
    IntPtr hdc 
= g.GetHdc();

    IImaging.IImagingFactory factory 
= IImaging.CreateFactory();
    IImaging.IImage image;
    factory.CreateImageFromFile(
@"\Storage Card\test.png"out image);
    Rectangle rect 
= new Rectangle(00, pictureBox1.Width, pictureBox1.Height);
    image.Draw(hdc, 
ref rect, IntPtr.Zero); 
    g.ReleaseHdc(hdc);
}

 

转载于:https://www.cnblogs.com/IamEasy_Man/archive/2010/01/07/1641011.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值