예제 #1
0
파일: ExportCtl.cs 프로젝트: xxxxst/gifEdit
        private PNG formatPng(ImageModel imgMd)
        {
            try {
                FIBITMAP dib      = FreeImage.Allocate(imgMd.width, imgMd.height, 32, 8, 8, 8);
                IntPtr   pFibData = FreeImage.GetBits(dib);
                Marshal.Copy(imgMd.data, 0, pFibData, imgMd.data.Length);

                //FIMEMORY fm = new FIMEMORY();
                FIMEMORY fm = FreeImage.OpenMemory(IntPtr.Zero, 0);
                FreeImage.SaveToMemory(FREE_IMAGE_FORMAT.FIF_PNG, dib, fm, FREE_IMAGE_SAVE_FLAGS.DEFAULT);

                FreeImage.SeekMemory(fm, 0, SeekOrigin.End);
                int bufferSize = FreeImage.TellMemory(fm);
                //Debug.WriteLine("aaa:" + bufferSize);

                FreeImage.SeekMemory(fm, 0, SeekOrigin.Begin);
                byte[] buffer = new byte[bufferSize];
                FreeImage.ReadMemory(buffer, (uint)bufferSize, (uint)bufferSize, fm);

                FreeImage.CloseMemory(fm);
                FreeImage.Unload(dib);

                //using(FileStream fs = new FileStream("aaa_" + i + ".png", FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)) {
                //	fs.Write(buffer, 0, bufferSize);
                //}

                //using(FileStream fs = new FileStream("aaa.png", FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)) {
                //	fs.Write(buffer, 0, bufferSize);
                //}

                PNG pngFrame = new PNG();
                using (MemoryStream ms = new MemoryStream(buffer)) {
                    pngFrame.Load(ms);
                }

                return(pngFrame);
            } catch (Exception) { }

            return(null);
        }
        public override Photo Decode(string file)
        {
            byte[] buffer = File.ReadAllBytes(file);
            int    nBufferLength = buffer.Length;
            uint   nImageWidth, nImageHeight;
            uint   nBitsPerPixel;
            uint   nBitsCount;
            //byte[] imageBits;
            NativePhoto photo = null;

            unsafe
            {
                fixed(byte *pBuffer = buffer)
                {
                    FIMEMORY              fiMemory  = FreeImage.OpenMemory((IntPtr)pBuffer, nBufferLength);
                    FREE_IMAGE_FORMAT     fiFormat  = FreeImage.GetFileTypeFromMemory(fiMemory, nBufferLength);
                    FIBITMAP              fiBitmap  = FreeImage.LoadFromMemory(fiFormat, fiMemory, 0);
                    FREE_IMAGE_COLOR_TYPE colorType = FreeImage.GetColorType(fiBitmap);

                    HBitmap  hBitmap;
                    HPalette hPalette = IntPtr.Zero, hOldPalette = IntPtr.Zero;

                    nImageHeight  = FreeImage.GetHeight(fiBitmap);
                    nImageWidth   = FreeImage.GetWidth(fiBitmap);
                    nBitsPerPixel = FreeImage.GetBPP(fiBitmap);

                    //if (nBitsPerPixel <= 8)
                    //{
                    //    RGBQUAD[] rgb = FreeImage.GetPaletteCopy(fiBitmap);
                    //    LOGPALETTE logPalette = new LOGPALETTE();
                    //    logPalette.nPalVersion = 0x300;
                    //    logPalette.nPalEntries = 1 << 8;
                    //    PaletteEntry[] palEntries = new PaletteEntry[logPalette.nPalEntries];
                    //    for (int i = 0; i < logPalette.nPalEntries; i++)
                    //    {
                    //        palEntries[i].Blue = rgb[i].rgbBlue;
                    //        palEntries[i].Green = rgb[i].rgbGreen;
                    //        palEntries[i].Red = rgb[i].rgbRed;
                    //    }
                    //    fixed (PaletteEntry* pPalletteEntries = palEntries)
                    //    {
                    //        logPalette.pPalEntry = (IntPtr)pPalletteEntries;
                    //        hPalette = CreatePalette(&logPalette);
                    //    }
                    //}

                    HDC hDC = GetDC(IntPtr.Zero);

                    Debug.Assert(hDC != IntPtr.Zero);
                    //if (hPalette != IntPtr.Zero)
                    //{
                    //    hOldPalette = SelectPalette(hDC, hPalette, false);
                    //    RealizePalette(hDC);
                    //}
                    hBitmap = CreateCompatibleBitmap(hDC, (int)nImageWidth, (int)nImageHeight);
                    //    hBitmap = CreateDIBitmap(hDC, FreeImage.FreeImage_GetInfoHeader(fiBitmap), 0x04,
                    //FreeImage.GetBits(fiBitmap), FreeImage.FreeImage_GetInfo(fiBitmap), ColorUseOptions.DIB_PAL_COLORS);
                    int nResult = SetDIBits(hDC, hBitmap, 0, nImageHeight, FreeImage.GetBits(fiBitmap),
                                            FreeImage.FreeImage_GetInfo(fiBitmap), ColorUseOptions.DIB_RGB_COLORS);

                    Debug.Assert(nResult != 0);

                    if (hOldPalette != IntPtr.Zero)
                    {
                        SelectPalette(hDC, hOldPalette, false);
                    }

                    int width  = (int)(FreeImage.GetWidth(fiBitmap));
                    int height = (int)(FreeImage.GetHeight(fiBitmap));

                    photo = new NativePhoto(hBitmap, width, height);

                    ReleaseDC(IntPtr.Zero, hDC);
                    //if (hPalette != IntPtr.Zero)
                    //    info.PaletteHandle = hPalette;

                    FreeImage.Unload(fiBitmap);
                    FreeImage.CloseMemory(fiMemory);
                }
            }
            return(photo);
        }