首页 > 解决方案 > Image.FromFile("image_path") 抛出一些 JPEG 文件的内存不足异常

问题描述

从外部 URL 下载图像后,我正在生成缩略图。url 包含 JPEG 图像的路径。当我为 jpeg 图像创建缩略图时,已经为某些 JPEG 图像生成了缩略图,而没有为某些 JPEG 图像生成缩略图。我无法找到为什么它不适用于某些 JPEG 图像的根本原因。下面是相同的代码片段。

        try
        {
            Image image = Image.FromFile("image_Path");

            int srcWidth = image.Width;
            int srcHeight = image.Height;
            int thumbWidth = width;
            int thumbHeight = 0;
            Bitmap bmp = new Bitmap(72, 72);

            if (srcHeight > srcWidth)
            {
                thumbHeight = (srcHeight / srcWidth) * thumbWidth;
                bmp = new Bitmap(thumbWidth, thumbHeight);
            }
            else
            {
                thumbHeight = thumbWidth;
                thumbWidth = (srcWidth / srcHeight) * thumbHeight;
                bmp = new Bitmap(thumbWidth, thumbHeight);
            }

            Graphics gr = Graphics.FromImage(bmp);
            gr.SmoothingMode = SmoothingMode.HighQuality;
            gr.CompositingQuality = CompositingQuality.HighQuality;
            gr.InterpolationMode = InterpolationMode.High;
            Rectangle rectDestination = new Rectangle(0, 0, thumbWidth, thumbHeight);
            gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
            bmp.Save(destinationfile);
            bmp.Dispose();
            image.Dispose();
        }
        catch (OutOfMemoryException ex)
        {

        }
        finally
        {
            //bmp.Dispose();
            //image.Dispose();
        }

标签: c#asp.net-mvc

解决方案


推荐阅读