GdipLoadImage (gdi32)
Last changed: anonymous

.
Summary
TODO - GdipLoadImageFromFile loads an image about 100x faster than System.Drawing because it does not perform any image validation.

C# Signature:

[DllImport("gdiplus.dll", CharSet=CharSet.Unicode)]
public static extern int GdipLoadImageFromFile(string filename, out IntPtr image);

VB Signature:

User-Defined Types:

None.

Notes:

You must have SecurityPermissionFlag.UnmanagedCode to run this code.

Tips & Tricks:

Sample Code:

    public class FastImageGdiPlus
    {
        [DllImport("gdiplus.dll", CharSet=CharSet.Unicode)]
        public static extern int GdipLoadImageFromFile(string filename, out IntPtr image);

        private FastImageGdiPlus()
        {
        }

        private static Type imageType = typeof(System.Drawing.Bitmap);

        [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
        internal static Image FastFromFile(string filename)
        {
            try
            {
                filename = Path.GetFullPath(filename);
                IntPtr loadingImage = IntPtr.Zero;

                // We are not using ICM at all, fudge that, this should be FAAAAAST!
                if ( GdipLoadImageFromFile(filename, out loadingImage) != 0 )
                {
                    throw new Exception("GDI+ threw a status error code.");
                }

                return (Bitmap) imageType.InvokeMember("FromGDIplus", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.InvokeMethod, null, null, new object[] { loadingImage });
            }
            catch(SecurityException)
            {
                return Image.FromFile(filename);
            }
        }
    }

Alternative Managed API:

System.Drawing.Image.FromFile.

Documentation