Esempio n. 1
0
        public static Bitmap FromFile(string filename)
        {
            if (string.IsNullOrEmpty(filename))
            {
                return(null);
            }

            if (FileOperations.FileExists(filename))
            {
                if (FileOperations.FileExtensionIs(filename, ".png"))
                {
                    int    w = 0;
                    int    h = 0;
                    IntPtr ppvBits;
                    IntPtr hBitmap = NativeMethods.LoadBitmapPNG(filename, ref w, ref h, out ppvBits);
                    if (hBitmap != IntPtr.Zero)
                    {
                        Bitmap     result = new Bitmap(w, h, PixelFormat.Format32bppArgb);
                        BitmapData data   = result.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
                        NativeMethods.RtlMoveMemory(data.Scan0, ppvBits, h * data.Stride); // copies the bitmap
                        result.UnlockBits(data);
                        NativeMethods.DeleteObject(hBitmap);
                        result = BitmapPainter.ConvertTo32Bit(result, true);
                        return(result);
                    }
                }

                if (FileOperations.FileExtensionIs(filename, ".jpg") || FileOperations.FileExtensionIs(filename, ".jpeg"))
                {
                    int    w = 0;
                    int    h = 0;
                    IntPtr ppvBits;
                    IntPtr hBitmap = NativeMethods.LoadBitmapJPG(filename, ref w, ref h, out ppvBits);
                    if (hBitmap != IntPtr.Zero)
                    {
                        Bitmap     result = new Bitmap(w, h, PixelFormat.Format24bppRgb);
                        BitmapData data   = result.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                        NativeMethods.RtlMoveMemory(data.Scan0, ppvBits, h * data.Stride); // copies the bitmap
                        result.UnlockBits(data);
                        NativeMethods.DeleteObject(hBitmap);
                        Bitmap realResult = BitmapPainter.ConvertTo32Bit(result, true);
                        return(realResult);
                    }
                }

                if (FileOperations.FileExtensionIs(filename, ".ico"))
                {
                    Icon   icon   = new Icon(FileOperations.StripFileName(filename));
                    Bitmap result = icon.ToBitmap();
                    icon.Dispose();
                    return(result);
                }

                Bitmap image = null;
                IntPtr zero  = IntPtr.Zero;
                int    imageType;

                if (NativeMethods.GdipLoadImageFromFile(FileOperations.StripFileName(filename), out zero) != 0)
                {
                    return(null);
                }

                if (NativeMethods.GdipGetImageType(zero, out imageType) != 0)
                {
                    return(null);
                }

                if (imageType == 1)
                {
                    image = (Bitmap)typeof(System.Drawing.Bitmap).InvokeMember("FromGDIplus", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.InvokeMethod, null, null, new object[] { zero });
                }

                if (!NativeMethods.IsAnimatedGIF(filename))
                {
                    image = BitmapPainter.ConvertTo32Bit(image, true);
                }

                return(image);
            }
            else
            {
                return(null);
            }
        }