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

Source: Internet
Author: User

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/

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.