Another post for ASP.NET developers reading this blog. If you think these posts do not belong here, please leave a comment, and I'll consider moving my development articles to a separate blog.Any ASP.NET developer who tries to create "on-the-fly" image-thumbnails in his web-application, sooner or later faces this crappy annoying "out of memory" exception, that is thrown when you use
Image.FromStream
or Image.GetThumbnailImage
. Sometimes it is thrown when working with big (>5Mb) images, sometimes it's thrown for all images.Graphics.DrawImage
instead of Image.GetThumbnailImage
, another suggested creating a temp file first and then load Image
from it...Image.FromStream
method keep the stream open for the lifetime of your Image
.Image img = Image.FromStream(mystream, true, false);
//... some code
Image thumb = img.GetThumbnailImage(w, h, null, IntPtr.Zero);
//... some code
img.Dispose();
mystream.Close();
GetThumbnailImage
. In case you really need to close that stream while continuing working with the image, consider using Image.Clone()
, but I'm not sure this will help...validateImageData
to false
when calling the FromStream
method.FromStream
on large images. This one I actually found on the Microsoft's website - http://support.microsoft.com/kb/831419... Anyway, here's the code://note the third parameter set to "false"!!!
Image img = Image.FromStream(mystream, true, false);
Alex Yumashev
Alex has founded Jitbit in 2005 and is a software engineer passionate about customer support.