Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

GdipLoadImage (gdi32)
 
.
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

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions