예제 #1
0
        public static Bitmap GetDrawingBitmap(this IntPtr dibBitmap)
        {
            Bitmap finalImg = null;

            if (dibBitmap != IntPtr.Zero && IsDib(dibBitmap))
            {
                Bitmap tempImg = null;
                try
                {
                    BITMAPINFOHEADER header = (BITMAPINFOHEADER)Marshal.PtrToStructure(dibBitmap, typeof(BITMAPINFOHEADER));

                    if (header.Validate())
                    {
                        PixelFormat format = header.GetDrawingPixelFormat();
                        tempImg = new Bitmap(header.biWidth, Math.Abs(header.biHeight), header.GetStride(), format, header.GetScan0(dibBitmap));
                        ColorPalette pal = header.GetDrawingPalette(dibBitmap);
                        if (pal != null)
                        {
                            tempImg.Palette = pal;
                        }
                        float xdpi = header.GetXDpi();
                        float ydpi = header.GetYDpi();
                        if (xdpi != 0 && ydpi == 0)
                        {
                            ydpi = xdpi;
                        }
                        else if (ydpi != 0 && xdpi == 0)
                        {
                            xdpi = ydpi;
                        }
                        if (xdpi != 0)
                        {
                            tempImg.SetResolution(xdpi, ydpi);
                        }
                        if (header.IsBottomUpImage)
                        {
                            tempImg.RotateFlip(RotateFlipType.RotateNoneFlipY);
                        }
                        finalImg = tempImg;
                        tempImg  = null;
                    }
                }
                finally
                {
                    if (tempImg != null)
                    {
                        tempImg.Dispose();
                    }
                }
            }
            return(finalImg);
        }