Asp. NET output PNG image with GDI + General error resolution

Source: Internet
Author: User

I used ASP to generate a code picture with the JPG format, today want to change it to PNG format, the results appear GDI + General error, check the n-Long data, only to find the solution, to share this solution of netizens deep gratitude

Response.Clear ();
Response.ContentType = "Image/png";
IMG. Save (Response.outputstream, chartformat.png); The
unexpectedly appears to be an exception, which is GDI + generic error. But if the format is Response.ContentType = "Image/jpeg"; The
will not error.


has been encountered before, changed to
Response.ContentType = "Image/png";
 using (MemoryStream ms = new MemoryStream ())
 {
         Img. Save (MS, Chartformat.png);
        response.outputstream.write (Ms. GetBuffer (), 0, (int) Ms. Length);
 }

To enter a PNG image.
This is due to the inability of the Response.outputstream stream to be read backwards, that is, its CanSeek property
is false. PNG image generation is not like JPEG, not streaming, has been written to no longer tube, but need to go back to
constantly write structure data. But response flow can't go back to seek, so the direct use is not. Change to a MemoryStream that can
seek, sir, into a good PNG image, and then output to the response stream.

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.