C # memory exception throwing in GetThumbnailImage on server

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 the test server, I always get an out of memory exception at line: using (System.Drawing.Image thumbnail = image.GetThumbnailImage (m_thumbnailWidth, thHeight, () => false, IntPtr. Zero)) It does not manipulate a large image: it tries to convert a 480 * 640 image to a 96 * 128 thumbnail. I don’t know how to investigate / solve this problem. Anyone have any suggestions? This always happens, even after restarting IIS. At first I thought the image might be corrupted, but the dimensions are correct. Thank.

+3


source to share


2 answers


We also had problems with GDI + Operations on our ASP.Net server. Your mention of your code running on the server made me think it might be the same problem.

Remember that using classes in System.Drawing

Namespace is not supported on the server. The fatal thing is that it can run for a while and suddenly (even without code changes) errors pop up.

We had to rewrite a significant part of our server code.



See the comment:

Attention

Classes in the System.Drawing namespace are not supported for use in the Windows Service or ASP.NET. Attempting to use these classes from one of these types of applications may encounter unexpected problems such as reduced service efficiency and run-time exceptions. For a supported alternative, see Windows Imaging Components.

Source: http://msdn.microsoft.com/de-de/library/system.drawing(v=vs.110).aspx

+5


source


Many thanks to @vcsjones for pointing me in the right direction. Instead of using image.GetThumbnailImage, I call:

public static Image ResizeImage(Image imgToResize, Size size)
    {
        return (Image)(new Bitmap(imgToResize, size));
    }

      

Now a tricky line of code comes up:



using(Image thumbnail = ResizeImage(image, new Size(m_thumbnailWidth, thHeight)))

      

I got this line from Resize Image C # Now it works!

+4


source







All Articles