Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.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
C# FromFile(“Image”u path)抛出某些JPEG文件的内存不足异常_C#_Asp.net Mvc - Fatal编程技术网

C# FromFile(“Image”u path)抛出某些JPEG文件的内存不足异常

C# FromFile(“Image”u path)抛出某些JPEG文件的内存不足异常,c#,asp.net-mvc,C#,Asp.net Mvc,我从外部URL下载图像后生成缩略图。url具有JPEG图像的路径。当我为jpeg图像创建缩略图时,已经为某些jpeg图像生成了缩略图,而没有为某些jpeg图像生成缩略图。我无法找到根本原因,为什么它不工作的一些JPEG图像。下面是同样的代码片段 try { Image image = Image.FromFile("image_Path"); int srcWidth = image.Width

我从外部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();
        }

一个“问题”是创建位图
bitmap bmp=新位图(72,72)然后创建另一个
bmp=新位图(拇指宽度、拇指高度)不处理前一个
位图bmp=新位图(72,72)应该是
位图bmp取而代之。如果不是必需的,就不要分配。@Kwiksilver我从第一行得到异常。Image=Image.FromFile(“Image_路径”);如果您阅读了的文档,您将在异常部分看到它提到了引发异常的原因。原因是
文件没有有效的图像格式。
-或-
GDI+不支持文件的像素格式。
我猜您的
图像路径“
不是有效的图像。