When you use. NET as an image watermark, if the original image is in GIF format, you may encounter
The error "unable to create a graphics object from an image with indexed pixel format" appears. The corresponding English error message is "a graphics object cannot be
Created from an image that has an indexed pixel format"
This exception occurs inSystem. Drawing. Graphics G = system. Drawing. Graphics. fromimage("Image path")
By querying msdn, we can see the following prompt information:
To avoid this problem, you can clone the GIF image to a BMP file first:
Using (Image sourceimage = image. fromfile (" Original Image path "))
{
/// Determine whether the original image is a GIF Image
If (Sourceimage. rawformat. Equals (system. Drawing. imaging. imageformat. GIF ))
{
Bitmap BMP = New Bitmap (sourceimage. Width, sourceimage. Height, pixelformat. format32bppargb );
Using (Graphics G = graphics. fromimage (BMP ))
{
/// Improve the image quality
G. interpolationmode = system. Drawing. drawing2d. interpolationmode. highqualitybicubic;
G. smoothingmode = system. Drawing. drawing2d. smoothingmode. highquality;
G. compositingquality = system. Drawing. drawing2d. compositingquality. highquality;
G. drawimage (sourceimage, 0, 0 );
}
//// The current BMP replaces the original GIF image. The following operations will all be performed on this BMP.
}
}
After the above conversion, you can avoid exceptions caused by the image index information.
Original article: http://www.zu14.cn/2008/12/19/net_gif_index_error/