C#出GetThumbnailImage内存异常的服务器上 [英] C# out of memory exception in GetThumbnailImage on a server

查看:201
本文介绍了C#出GetThumbnailImage内存异常的服务器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行下面的代码,当用户向我们发送的图像创建缩略图:

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);
    }

这工作正常我的机器上,但是当我在测试服务器上运行它我总是上线内存不足的异常:使用(System.Drawing.Image对象缩略图= image.GetThumbnailImage(m_thumbnailWidth,thHeight,()=>假,IntPtr.Zero))$ b $
b是没有操纵一个大的图像:这是试图将480×640的图像转换成96 * 128缩略图。我不知道如何调查/解决这个问题。有没有人有什么建议吗?它总是会发生,即使我已经重新启动IIS。我最初的想象,图像可能已损坏,但尺寸是正确的。
感谢。

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.

推荐答案

我们也有我们的ASP.Net服务器使用GDI +操作类似的问题。您在服务器上运行代码的提及让我觉得,这可能是同样的问题。

We also had similar problems using GDI+ Operations in our ASP.Net server. Your mention of your code running on a server made me think, that this could be the same problem.

请注意,该班在<$ c中的用法$ C> System.Drawing中命名空间不支持的服务器上。致命的是,它可以工作了一段时间,突然(甚至无需更改代码)发生错误。

Please be aware, that the usage of classes in the System.Drawing Namespace is not supported on a server. The fatal thing is, that it may work for some time and suddenly (even without code changes) errors occur.

我们不得不改写我们的服务器代码显著的一部分。

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

查看评论:

警告说明

类是不支持Windows或ASP.NET服务中使用
。尝试
使用这些应用程序类型之一中这些类可以
产生意想不到的问题,如减少的服务性能
和运行时异常。对于支持的替代方法,请参阅Windows
成像组件

Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions. For a supported alternative, see Windows Imaging Components.

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

这篇关于C#出GetThumbnailImage内存异常的服务器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆