ImageGlass.Library.Image.ExifThumbReader.ReadThumb C# (CSharp) Method

ReadThumb() public static method

Reads the thumbnail in the given image. If no thumbnail is found, returns null
public static ReadThumb ( string imagePath ) : System.Drawing.Image
imagePath string
return System.Drawing.Image
        public static System.Drawing.Image ReadThumb(string imagePath)
        {
            const int GDI_ERR_PROP_NOT_FOUND = 19;  // Property not found error
            const int GDI_ERR_OUT_OF_MEMORY = 3;

            IntPtr hImage = IntPtr.Zero;
            IntPtr buffer = IntPtr.Zero;    // Holds the thumbnail data
            int ret;
            ret = GdipLoadImageFromFile(imagePath, out hImage);

            try
            {
                if (ret != 0)
                    throw createException(ret);

                int propSize;
                ret = GdipGetPropertyItemSize(hImage, THUMBNAIL_DATA, out propSize);
                // Image has no thumbnail data in it. Return null
                if (ret == GDI_ERR_PROP_NOT_FOUND)
                    return null;
                if (ret != 0)
                    throw createException(ret);

                // Allocate a buffer in memory
                buffer = Marshal.AllocHGlobal(propSize);
                if (buffer == IntPtr.Zero)
                    throw createException(GDI_ERR_OUT_OF_MEMORY);

                ret = GdipGetPropertyItem(hImage, THUMBNAIL_DATA, propSize, buffer);
                if (ret != 0)
                    throw createException(ret);

                // buffer has the thumbnail data. Now we have to convert it to
                // an Image
                return convertFromMemory(buffer);
            }

            finally
            {
                // Free the buffer
                if (buffer != IntPtr.Zero)
                    Marshal.FreeHGlobal(buffer);

                GdipDisposeImage(hImage);
            }
        }