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 }} |