Unable to create graphics object from image with indexed pixel format

Source: Internet
Author: User

Everyone is using it. net, you may encounter the error of "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:

Through the above error explanation, we can see that the reason is that the image is indexed in pixel format. To avoid this problem, we can determine whether the original image is indexed in pixel format before making the watermark. If yes, you can clone the image to a BMP file first:

 /// <Summary>  /// The pixelformat of the graphics exception is generated.  /// </Summary>  Private   Static Pixelformat [] indexedpixelformats = {pixelformat. undefined, pixelformat. dontcare, pixelformat. parameters, pixelformat. format1bppindexed, pixelformat. format4bppindexed, pixelformat. format8bppindexed }; /// <Summary>  /// Determine whether the pixelformat of the image is in the pixelformat that causes an exception  /// </Summary>  /// <Param name = "imgpixelformat"> pixelformat of the original image </param>  /// <Returns> </returns>  Private   Static   Bool Ispixelformatindexed (pixelformat imgpixelformat ){ Foreach (Pixelformat pfIn Indexedpixelformats ){ If (PF. Equals (imgpixelformat )) Return   True ;} Return   False ;} // ...... Use  Using (Image IMG = image. fromfile (" Original Image path ")){ // If the original image is indexed in pixel format, it needs to be converted.      If (Ispixelformatindexed (IMG. pixelformat) {bitmap BMP = New Bitmap (IMG. Width, IMG. Height, pixelformat. format32bppargb ); Using (Graphics G = graphics. fromimage (BMP) {G. interpolationmode = system. drawing. drawing2d. interpolationmode. highqualitybicubic; G. smoothingmode = system. drawing. drawing2d. smoothingmode. highquality; G. compositingquality = system. drawing. drawing2d. compositingquality. highquality; G. drawimage (IMG, 0, 0 );} // The following watermark operation directly performs the BMP operation.          //...... } Else   // Otherwise, perform the operation directly. { // Directly watermark img }}

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.