The solution to the common error of GDI + occurs when ASP. NET outputs PNG images,
Even the original ASP. NET uses JPG format to generate a verification code image. Today, if you want to change it to PNG format, a general error of GDI + occurs. After checking the N-long data, you can find the solution, I am very grateful to anyone who shared this solution.
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 Response. ContentType = "image/jpeg ";
No error will be reported.
Fortunately, I used to change it
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.