Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
服务器上GetThumbnailImage中的C#内存不足异常_C#_Image_Iis_Server - Fatal编程技术网

服务器上GetThumbnailImage中的C#内存不足异常

服务器上GetThumbnailImage中的C#内存不足异常,c#,image,iis,server,C#,Image,Iis,Server,我正在运行以下代码,以便在用户向我们发送图像时创建缩略图: public int AddThumbnail(byte[] originalImage, File parentFile) { File tnFile = null; try { System.Drawing.Image image; using (System.IO.MemoryStream memoryStream = new

我正在运行以下代码,以便在用户向我们发送图像时创建缩略图:

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,()=>false,IntPtr.Zero)) 它不是在操纵一个大图像:它试图将一个480*640的图像转换成一个96*128的缩略图。我不知道如何调查/解决这个问题。有人有什么建议吗?它总是发生,即使在我重新启动IIS之后。我最初确实认为图像可能已损坏,但尺寸是正确的。
谢谢。

非常感谢@vcsjones为我指明了正确的方向。不使用image.GetThumbnailImage,我调用:

public static Image ResizeImage(Image imgToResize, Size size)
    {
        return (Image)(new Bitmap(imgToResize, size));
    }
现在麻烦的代码行是:

using(Image thumbnail = ResizeImage(image, new Size(m_thumbnailWidth, thHeight)))
我从你那儿收到这封信
它现在起作用了

在ASP.Net服务器中使用GDI+操作时,我们也遇到了类似的问题。你提到你在服务器上运行的代码让我想到,这可能是同样的问题

请注意,服务器不支持在
System.Drawing
命名空间中使用类。致命的是,它可能会工作一段时间,然后突然(即使没有代码更改)出现错误

我们必须重写服务器代码的重要部分

见评论:

注意事项

System.Drawing命名空间中的类是 不支持在Windows或ASP.NET服务中使用。企图 要在这些应用程序类型中使用这些类,可能需要 产生意外问题,例如服务性能降低 和运行时异常。有关支持的备选方案,请参阅Windows 成像元件

资料来源:

服务器上的内存是否不足?拿出你的性能监视器,跟踪堆大小、GC Gen、总体内存消耗。随便猜测一下,也许你的机器是64位体系结构,而服务器是32位的,这意味着你的机器上有更多可用内存。即使内存没有问题,DI也可以抛出OOM。GDI通常在几乎任何错误条件下返回“分配内存失败”,即使由于其他原因分配失败,.NET库也会将其转换为OOM。这可能是一个合法的内存问题,但我见过许多其他人追逐这条白鲸,结果是其他原因(例如,编解码器的标志设置不好)。服务器是64位的。我很惊讶,操纵一个小图像会给我带来这个问题。我会看看内存消耗情况,汉克斯·斯特凡,我会检查一下。5年后,这仍然是相关的,我刚刚经历了一场噩梦