Everyone is using it. net, 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, 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") {// determines 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/