How to solve a common error caused by GDI + when ASP. NET outputs a PNG Image

Source: Internet
Author: User

The original text is as follows:
Code: CopyCode The Code is as follows: Response. Clear ();
Response. contenttype = "image/PNG ";
IMG. Save (response. outputstream, chartformat. PNG );

An exception occurred, which is a common error of GDI +. However, if the format is
Code:Copy codeThe Code is as follows: Response. contenttype = "image/JPEG ";

No error will be reported.
Fortunately, I used to change it
Code:Copy codeThe Code is as follows: Response. contenttype = "image/PNG ";
Using (memorystream MS = new memorystream ())
{
IMG. Save (MS, chartformat. PNG );
Response. outputstream. Write (Ms. getbuffer (), 0, (INT) ms. Length );
}

you can enter a PNG image.
This is because the stream response. outputstream cannot be read back, that is, its canseek attribute
is false. When a PNG image is generated, it is not like jpeg. It is not streaming. If it has been written, it is no longer necessary. Instead, it needs to go back
and constantly write structural data. However, the response stream cannot go back to seek, so it cannot be used directly. Change it to a memorystream that can be
seek. convert it into a PNG image and then output it 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.