GDI +中发生System.Drawing.Image.Save一般性错误 [英] A generic error occurred in GDI+ at System.Drawing.Image.Save

查看:917
本文介绍了GDI +中发生System.Drawing.Image.Save一般性错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例外:

Exception:

GDI +中发生一般性错误。    在System.Drawing.Image.Save(字符串文件名,图片codecInfo EN codeR,恩coderParameters EN coderParams)    在System.Drawing.Image.Save(字符串文件名,格式的imageformat)    在System.Drawing.Image.Save(字符串文件名)

A generic error occurred in GDI+. at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) at System.Drawing.Image.Save(String filename, ImageFormat format) at System.Drawing.Image.Save(String filename)

code:

Code:

byte[] bitmapData = new byte[imageText.Length];
MemoryStream streamBitmap;
bitmapData = Convert.FromBase64String(imageText);
streamBitmap = new MemoryStream(bitmapData);
System.Drawing.Image img = Image.FromStream(streamBitmap);
img.Save(path);

我们转换一个base64字符串到MemoryStream,然后创建一个System.Drawing.Image对象(Image.FromStream(streamBitmap))。 在结束的图像保存在一个临时文件。

We convert a base64 string into a MemoryStream and then create a System.Drawing.Image (Image.FromStream(streamBitmap)). At the end the image is saved in a temp file.

但奇怪的是,这个问题似乎发生时的活动(并发用户数)为高电平时,Web服务器上的问题是一个IISRESET或应用程序池回收后,暂时解决了......

The strange thing is that the problem seems to occur when the activity (number of concurrent users) is high on the web server and the problem is solved temporarily after an IISRESET or an application pool recycle...

==>垃圾收集器的问题?

==> Garbage collector issue ?

我已经检查TEMP文件夹的权限...

I already checked the permission of the TEMP folder...

推荐答案

在你加载imagefrom甲流,你必须保持流打开图像的一生中,看到的MSDN Image.FromStream 的。

When you are loading an imagefrom a Stream, You have to keep the stream open for the lifetime of the image, see MSDN Image.FromStream.

我觉得异常造成的,因为形象得到处理之前内存流被封闭。你可以改变你的code这样

I think the exception is caused because the memory stream gets closed even before the image gets disposed. You can change your code like this

byte[] bitmapData = new byte[imageText.Length];
bitmapData = Convert.FromBase64String(imageText);

  using (var streamBitmap = new MemoryStream(bitmapData)
  {
      using (img = Image.FromStream(streamBitmap))
      { 
         img.Save(path);
      }
  }

下面是一些链接到线程讨论类似的问题

Here are some links to threads discussing similar problems

GDI +错误保存从网页图片

<一个href="http://stackoverflow.com/questions/1772083/when-drawing-an-image-system-runtime-interopservices-externalexception-a-gener/1783778#1783778">When绘制图像:System.Runtime.InteropServices.ExternalException:GDI中发生一般性错误

这篇关于GDI +中发生System.Drawing.Image.Save一般性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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