I am using ImageProcessor to process images in my website.
I have this resize function:
public Image ResizePhoto6version(Image img, int width, int height)
{
using (var ms = new MemoryStream())
{
using (var imgf = new ImageFactory(false))
{
imgf.Load(img)
.Resize(new ResizeLayer(new Size(width, height), ResizeMode.Max))
.Save(ms);
return Bitmap.FromStream(ms);
}
}
}
In the webservice, I run this code:
MemoryStream ytSmallStream = new MemoryStream();
MemoryStream ytMediumStream = new MemoryStream();
System.Drawing.Image ytSmallThumb = null;
System.Drawing.Image ytMediumThumb = null;
ytSmallThumb.Save(ytSmallStream, ImageFormat.Jpeg);
ytSmallStream.Position = 0;
ytMediumThumb.Save(ytMediumStream, ImageFormat.Jpeg);
ytMediumStream.Position = 0;
I get an exception when it reached the Save function ytSmallThumb.Save():
A generic error occurred in GDI+
The image is returned correctly from ResizeThumbnailToSmall function and the Stream has information of the image with the right size.
FromStream
requires the underlying stream to remain open, yet is being disposed in theResizePhoto6version
method (by way of theusing
). From MSDN: "You must keep the stream open for the lifetime of the Image." msdn.microsoft.com/en-us/library/… – Cholecystotomyusing
from theResizePhoto6version
method (leave it simply asvar ms=new MemoryStream();
) . It's a memory stream, no need to dispose it really. See Jon's answer in the duplicate; just make sure toDispose
the bitmap/image when you're done and it will close the stream for you – Cholecystotomy