在Bitmap.Save方法中GDI +中发生了一般错误

don*_*ack 58 c# system.drawing gdi+

我正在努力上传并将该图像的缩略图副本保存在缩略图文件夹中.

我正在使用以下链接:

http://weblogs.asp.net/markmcdonnell/archive/2008/03/09/resize-image-before-uploading-to-server.aspx

newBMP.Save(directory + "tn_" + filename);   
Run Code Online (Sandbox Code Playgroud)

导致异常"GDI +中发生了一般错误."

我试图给予文件夹权限,还试图在保存时使用新的单独的bmp对象.

编辑:

    protected void ResizeAndSave(PropBannerImage objPropBannerImage)
    {
        // Create a bitmap of the content of the fileUpload control in memory
        Bitmap originalBMP = new Bitmap(fuImage.FileContent);

        // Calculate the new image dimensions
        int origWidth = originalBMP.Width;
        int origHeight = originalBMP.Height;
        int sngRatio = origWidth / origHeight;
        int thumbWidth = 100;
        int thumbHeight = thumbWidth / sngRatio;

        int bannerWidth = 100;
        int bannerHeight = bannerWidth / sngRatio;

        // Create a new bitmap which will hold the previous resized bitmap
        Bitmap thumbBMP = new Bitmap(originalBMP, thumbWidth, thumbHeight);
        Bitmap bannerBMP = new Bitmap(originalBMP, bannerWidth, bannerHeight);

        // Create a graphic based on the new bitmap
        Graphics oGraphics = Graphics.FromImage(thumbBMP);
        // Set the properties for the new graphic file
        oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

        // Draw the new graphic based on the resized bitmap
        oGraphics.DrawImage(originalBMP, 0, 0, thumbWidth, thumbHeight);

        Bitmap newBitmap = new Bitmap(thumbBMP);
        thumbBMP.Dispose();
        thumbBMP = null;

        // Save the new graphic file to the server
        newBitmap.Save("~/image/thumbs/" + "t" + objPropBannerImage.ImageId, ImageFormat.Jpeg);

        oGraphics = Graphics.FromImage(bannerBMP);
        // Set the properties for the new graphic file
        oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

        // Draw the new graphic based on the resized bitmap
        oGraphics.DrawImage(originalBMP, 0, 0, bannerWidth, bannerHeight);
        // Save the new graphic file to the server
        bannerBMP.Save("~/image/" + objPropBannerImage.ImageId + ".jpg");


        // Once finished with the bitmap objects, we deallocate them.
        originalBMP.Dispose();

        bannerBMP.Dispose();
        oGraphics.Dispose();
    }
Run Code Online (Sandbox Code Playgroud)

NSG*_*aga 87

当从文件构造Bitmap对象或Image对象时,该文件在对象的生命周期内保持锁定状态.因此,您无法更改图像并将其保存回原始位置的同一文件中. http://support.microsoft.com/?id=814675

GDI +,JPEG Image to MemoryStream中发生一般错误

Image.Save(..)抛出GDI +异常,因为内存流已关闭

http://alperguc.blogspot.in/2008/11/c-generic-error-occurred-in-gdi.html

编辑:
只是从记忆中写作......

保存到"中间"内存流,这应该工作

例如试试这个 - 替换

    Bitmap newBitmap = new Bitmap(thumbBMP);
    thumbBMP.Dispose();
    thumbBMP = null;
    newBitmap.Save("~/image/thumbs/" + "t" + objPropBannerImage.ImageId, ImageFormat.Jpeg);
Run Code Online (Sandbox Code Playgroud)

有类似的东西:

string outputFileName = "...";
using (MemoryStream memory = new MemoryStream())
{
    using (FileStream fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite))
    {
        thumbBMP.Save(memory, ImageFormat.Jpeg);
        byte[] bytes = memory.ToArray();
        fs.Write(bytes, 0, bytes.Length);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 一个相关的问题,也许它会帮助某人 - 我已经在_opening_位图上得到了"GDI +中出现了一般错误".并且仅在远程网络服务器上,而不是在我的本地测试机器上.事实证明,Azure IIS服务器上存在的GDI +无法处理GIMP生成的新格式BMP.我不得不a)使用Paint重新保存BMP b)使用未压缩的PNG而不是c)使用GIMP保存为24位BMP(质量更差) (2认同)

Reg*_*dit 44

如果传递给的路径Bitmap.Save()无效(文件夹不存在等),也会显示此错误消息.

  • 这个错误只能报告这样一个基本问题是多么愚蠢 (16认同)
  • 当然这就是我的情况,我投了票,所以没有人会浪费任何更多的时间.还要确保您的路径也包含文件名. (6认同)

Han*_*ant 10

    // Once finished with the bitmap objects, we deallocate them.
    originalBMP.Dispose();

    bannerBMP.Dispose();
    oGraphics.Dispose();
Run Code Online (Sandbox Code Playgroud)

这是一种你迟早会后悔的编程风格.早点敲门,你忘了一个.您没有处置newBitmap.在垃圾收集器运行之前,它会锁定文件.如果它没有运行,那么第二次尝试保存到同一个文件时,你将获得klaboom.GDI +异常过于悲惨,无法提供良好的诊断,因此严重的头疼.除了提到这个错误的成千上万的可搜索帖子之外.

总是喜欢使用using语句.即使代码抛出异常,也永远不会忘记处置对象.

using (var newBitmap = new Bitmap(thumbBMP)) {
    newBitmap.Save("~/image/thumbs/" + "t" + objPropBannerImage.ImageId, ImageFormat.Jpeg);
}
Run Code Online (Sandbox Code Playgroud)

虽然我们甚至不清楚为什么你甚至创建一个新的位图,但保存thumbBMP应该已经足够好了.Anyhoo,用爱来给你的一次性物品一样.


Pal*_*mar 8

在我的情况下,位图图像文件已经存在于系统驱动器中,因此我的应用程序抛出错误"GDI +中出现了一般错误".

  1. 验证目标文件夹是否存在
  2. 验证目标文件夹中是否已存在具有相同名称的文件


小智 7

检查文件夹的权限,保存图像右侧cLick文件夹然后转到:

属性>安全性>编辑>添加 - 选择"所有人"并选中允许"完全控制"


Anj*_*ant 5

我遇到了同样的问题在使用MVC应用程序时,保存时在GDI +中发生了一般性错误,由于出现了错误的保存图像路径,我更正了保存路径,因此对我来说很好,这是我遇到的错误。

img1.Save(Server.MapPath("/Upload/test.png", System.Drawing.Imaging.ImageFormat.Png);


--Above code need one change, as you need to put close brackets on Server.MapPath() method after writing its param.
Run Code Online (Sandbox Code Playgroud)

像这样-

img1.Save(Server.MapPath("/Upload/test.png"), System.Drawing.Imaging.ImageFormat.Png);
Run Code Online (Sandbox Code Playgroud)