How to improve the code below in terms of better performance?
For example, must I explicitly dispose both MemoryStream
and Image
objects after calling OutputStream.Write()
?
Any improvement on other areas, e.g. Buffer
and MemoryStream
allocation?
The code is to allow a web user to download a PNG file. Part of it is below:
OutputImage(image);
image.Dispose();
...
public void OutputImage(Image image){
using(MemoryStream temp = new MemoryStream()){
image.Save(temp, ImageFormat.Png)
byte[] buffer = temp.GetBuffer();
context.Response.OutputStream.Write(buffer, 0, temp.Length);
}
}
ASP.NET, .NET 4.5
Response.OutputStream GetBuffer