The original text is as follows:
Code:
Copy codeThe 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 already been written, it is not necessary to go back.
Write structural data constantly. However, the response stream cannot go back to seek, so it cannot be used directly. Can be changed to one.
Seek's MemoryStream, which is converted into a png image and then output to the response stream.