C#编程->FreeImage.dll使用方法

       

         最近用到这个FreeImage.DLL,之前只是在C++里面调用,现在需要在C#里调用,于是学习了一点点,总结一下:

在FreeImage中,要用到几个参数,在C#中定义如下:

private enum FREE_IMAGE_FORMAT
        {
            FIF_UNKNOWN = -1,
            FIF_BMP = 0,
            FIF_ICO = 1,
            FIF_JPEG = 2,
            FIF_JNG = 3,
            FIF_KOALA = 4,
            FIF_LBM = 5,
            FIF_IFF = FIF_LBM,
            FIF_MNG = 6,
            FIF_PBM = 7,
            FIF_PBMRAW = 8,
            FIF_PCD = 9,
            FIF_PCX = 10,
            FIF_PGM = 11,
            FIF_PGMRAW = 12,
            FIF_PNG = 13,
            FIF_PPM = 14,
            FIF_PPMRAW = 15,
            FIF_RAS = 16,
            FIF_TARGA = 17,
            FIF_TIFF = 18,
            FIF_WBMP = 19,
            FIF_PSD = 20,
            FIF_CUT = 21,
            FIF_XBM = 22,
            FIF_XPM = 23,
            FIF_DDS = 24,
            FIF_GIF = 25,
            FIF_HDR = 26
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct RGBQUAD
        {
            internal byte Blue;
            internal byte Green;
            internal byte Red;
            internal byte Reserved;
        }
接下来,对DLL中的函数进行申明如下:

        [DllImport("FreeImage.dll", EntryPoint = "_FreeImage_Load@12", SetLastError = true)]
        private static extern IntPtr FreeImage_Load(FREE_IMAGE_FORMAT fif, string FileName, int Flag);

        [DllImport("FreeImage.dll", EntryPoint = "_FreeImage_GetFileType@8", SetLastError = true)]
        private static extern FREE_IMAGE_FORMAT FreeImage_GetFileType(string FileName, int Size);

        [DllImport("FreeImage.dll", EntryPoint = "_FreeImage_GetFIFFromFilename@4", SetLastError = true)]
        private static extern FREE_IMAGE_FORMAT FreeImage_GetFIFFromFilename(string FileName);

        [DllImport("FreeImage.dll", EntryPoint = "_FreeImage_FIFSupportsReading@4", SetLastError = true)]
        private static extern FREE_IMAGE_FORMAT FreeImage_FIFSupportsReading(FREE_IMAGE_FORMAT fif);

        [DllImport("FreeImage.dll", EntryPoint = "_FreeImage_GetWidth@4", SetLastError = true)]
        private static extern int FreeImage_GetWidth(IntPtr Dib);

        [DllImport("FreeImage.dll", EntryPoint = "_FreeImage_GetHeight@4", SetLastError = true)]
        private static extern int FreeImage_GetHeight(IntPtr Dib);

        [DllImport("FreeImage.dll", EntryPoint = "_FreeImage_GetPalette@4", SetLastError = true)]
        private static extern RGBQUAD* FreeImage_GetPalette(IntPtr Dib);

        [DllImport("FreeImage.dll", EntryPoint = "_FreeImage_GetBPP@4", SetLastError = true)]
        private static extern int FreeImage_GetBPP(IntPtr Dib);

        [DllImport("FreeImage.dll", EntryPoint = "_FreeImage_GetDIBSize@4", SetLastError = true)]
        private static extern int FreeImage_GetDIBSize(IntPtr Dib);

        [DllImport("FreeImage.dll", EntryPoint = "_FreeImage_GetColorsUsed@4", SetLastError = true)]
        private static extern int FreeImage_GetColorsUsed(IntPtr Dib);

        [DllImport("FreeImage.dll", EntryPoint = "_FreeImage_GetPitch@4", SetLastError = true)]
        private static extern int FreeImage_GetPitch(IntPtr Dib);

        [DllImport("FreeImage.dll", EntryPoint = "_FreeImage_GetBits@4", SetLastError = true)]
        private static extern IntPtr FreeImage_GetBits(IntPtr Dib);

        [DllImport("FreeImage.dll", EntryPoint = "_FreeImage_Unload@4", SetLastError = true)]
        private static extern int FreeImage_Free(IntPtr Dib);

        [DllImport("FreeImage.dll", EntryPoint = "_FreeImage_FlipVertical@4", SetLastError = true)]
        private static extern int FreeImage_FlipVertical(IntPtr Dib);

接下来就可以调用FreeImage.dll中的函数了,用FreeImage.dll来加载一张图片,代码如下:

   Bitmap Bmp = null;
            FREE_IMAGE_FORMAT fif = FREE_IMAGE_FORMAT.FIF_UNKNOWN; ;
            fif = FreeImage_GetFileType(FileName, 0);//获取加载图像的格式
            if (fif == FREE_IMAGE_FORMAT.FIF_UNKNOWN)
            {
                fif = FreeImage_GetFIFFromFilename(FileName);
            }

            if ((fif != FREE_IMAGE_FORMAT.FIF_UNKNOWN) && (FreeImage_FIFSupportsReading(fif) != 0))
            {
                IntPtr Dib = FreeImage_Load(fif, FileName, 0);  //获取图像数据指针
                int Bpp = FreeImage_GetBPP(Dib);//获取图像深度
                PixelFormat PF;
                int Width, Height, Stride;
                switch (Bpp)
                {
                    case 1:
                        PF = PixelFormat.Format1bppIndexed; break;
                    case 4:
                        PF = PixelFormat.Format4bppIndexed; break;
                    case 8:
                        PF = PixelFormat.Format8bppIndexed; break;
                    case 16:
                        PF = PixelFormat.Format16bppRgb555; break;
                    case 24:
                        PF = PixelFormat.Format24bppRgb; break;
                    case 32:
                        PF = PixelFormat.Format32bppArgb; break;
                    default:
                        FreeImage_Free(Dib);
                        return null;
                }
                Width = FreeImage_GetWidth(Dib);                        //  图像宽度
                Height = FreeImage_GetHeight(Dib);                      //  图像高度
                Stride = FreeImage_GetPitch(Dib);                       //  图像扫描行的大小,必然是4的整数倍

如果我们想要将图像数据转为BYTE[]格式,有两种方式:

1.直接用copy函数

               int copycount = Width * Height * 4;
               
                IntPtr Bits = FreeImage_GetBits(Dib);

                byte[] line1 = new byte[Width * Height * 4];
                System.Runtime.InteropServices.Marshal.Copy(Bits, line1, 0, copycount);//copy 图像数据

2.用BitMap中内存操作函数

                byte[] line = new byte[Width * Height * 4];
                
                byte temp;
                 //32位 RGBA格式图像信息
                BitmapData bdata = Bmp.LockBits(new Rectangle(0, 0, Bmp.Width, Bmp.Height), ImageLockMode.ReadWrite, PF);
                {
                    unsafe {

                        byte* ptr = (byte*)(bdata.Scan0);
                        IntPtr ptr1 = bdata.Scan0;
                        System.Runtime.InteropServices.Marshal.Copy(ptr1,line,0,copycount);

                    }
                }
Bmp.UnlockBits(bdata);

以上就可以实现图像数据的操作了。






你可能感兴趣的:(C#编程->FreeImage.dll使用方法)