Unable to create a graphics object from an image with indexed pixel format | a graphics object cannot be created from an image that has an indexed pix

Source: Internet
Author: User

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.