I am running the below code to create a thumbnail when a user sends us an image:
public int AddThumbnail(byte[] originalImage, File parentFile)
{
File tnFile = null;
try
{
System.Drawing.Image image;
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(originalImage))
{
image = System.Drawing.Image.FromStream(memoryStream);
}
Log.Write("Original image width of [" + image.Width.ToString() + "] and height of [" + image.Height.ToString() + "]");
//dimensions need to be changeable
double factor = (double)m_thumbnailWidth / (double)image.Width;
int thHeight = (int)(image.Height * factor);
byte[] tnData = null;
Log.Write("Thumbnail width of [" + m_thumbnailWidth.ToString() + "] and height of [" + thHeight + "]");
using (System.Drawing.Image thumbnail = image.GetThumbnailImage(m_thumbnailWidth, thHeight, () => false, IntPtr.Zero))
{
using (System.IO.MemoryStream tnStream = new System.IO.MemoryStream())
{
thumbnail.Save(tnStream, System.Drawing.Imaging.ImageFormat.Jpeg);
tnData = new byte[tnStream.Length];
tnStream.Position = 0;
tnStream.Read(tnData, 0, (int)tnStream.Length);
}
}
//there is other code here that is not relevant to the problem
}
catch (Exception ex)
{
Log.Error(ex);
}
return (tnFile == null ? -1 : tnFile.Id);
}
This works fine on my machine, but when I run it on a test server I always get an out of memory exception on the line: using (System.Drawing.Image thumbnail = image.GetThumbnailImage(m_thumbnailWidth, thHeight, () => false, IntPtr.Zero)) It is not manipulating a large image: it is trying to convert a 480*640 image into a 96*128 thumbnail. I don't know how to investigate / resolve this issue. Has anyone got any suggestions? It always happens, even after I have restarted IIS. I did initially think that the image might be corrupt but the dimensions are correct. Thanks.