在处理图片时,读取本地图像文件,进行另存时发生GDI+中发生一般性错误 。

具体情况如下:

用OpenFileDialog打开图像文件,文件名为filename
StreamReader sr = new StreamReader(filename);
Stream sm = sr.BaseStream;
imgZheng = Image.FromStream(sm);sm.Close();
sr.Close();//将读取到的Image重新写入流,转化为byte[]以便进行进一步的处理
MemoryStream mmstream = new MemoryStream();
imgZheng.Save(mmstream, ImageFormat.Png);//执行到此处时会出现“GDI+中发生一般性错误”这个错误。byte[] imgByte = null;
imgByte =  mstream.ToArray();

原因:在Image.Save时也会发生这种错误,sm.Close(); sr.Close();引起。注销掉就可以正常运行,但是文件被锁定。不注销掉关闭流,再Save()将引起该错误。

解决方法如下:

byte[] ImgByte = null;MemoryStream mStream = new MemoryStream();Bitmap bmp = new Bitmap(filename);bmp.Save(mStream, ImageFormat.Png);ImgByte = mStream.ToArray();bmp.Dispose();mStream.Close();MemoryStream mstream = new MemoryStream(ImgByte);imgZheng = Image.FromStream(mstream);mstream.Close();
//此时再执行Save方法,将不会报错          MemoryStream mstream = new MemoryStream();                imgZheng.Save(mstream, ImageFormat.Png);

方法二:

            StreamReader sr = new StreamReader(filename);Stream sm = sr.BaseStream;Image img = Image.FromStream(sm);img = Image.FromStream(sm);
           //将图像序列化给指定的流,再将流序列化为二进制数组。BinaryFormatter binaryFormatter = new BinaryFormatter();MemoryStream mstream = new MemoryStream();binaryFormatter.Serialize(mstream, img);byte[] ImgByte = mstream.ToArray();mstream.Close();sm.Close();sr.Close();//将二进制数组放到流中,再反序列为图像MemoryStream memostream = new MemoryStream(ImgByte);imgZheng = (Image)binaryFormatter.Deserialize(memostream);memostream.Close();
//此时再执行Save方法,将不会报错          MemoryStream mstream = new MemoryStream();                imgZheng.Save(mstream, ImageFormat.Png);
 

方法三:(与方法二相比,流的赋值方式不一样,流转换为图像的方式也不一样)

StreamReader sr = new StreamReader(filename);Stream sm = sr.BaseStream;Image img = Image.FromStream(sm);img = Image.FromStream(sm);MemoryStream mstream = new MemoryStream();img.Save(mstream, ImageFormat.Png);byte[] ImgByte = mstream.ToArray();mstream.Close();sm.Close();sr.Close();MemoryStream mmstream = new MemoryStream(ImgByte);imgZheng = Image.FromStream(mmstream);mmstream.Close();//此时再执行Save方法,将不会报错          MemoryStream mstream = new MemoryStream();                imgZheng.Save(mstream, ImageFormat.Png);

转载于:https://www.cnblogs.com/chiyueqi/p/3225794.html

GDI+中发生一般性错误 Winform Image.Save(mstream, ImageFormat.Png)引发相关推荐

  1. Image.Save()发生“GDI+ 中发生一般性错误”

    从数据库中读取的图片是byte[]类型,将其转换成Image可以正常显示,但是调用image.Save()时会发生"GDI+ 中发生一般性错误". public static Sy ...

  2. C#界面设计--5--Bitmap.save保存图片时: GDI+ 中发生一般性错误 解决办法

    Bitmap.save保存图片时: GDI+ 中发生一般性错误 解决办法 源程序: var date = DateTime.Now.ToString("yyyy-MM-dd");/ ...

  3. GDI+中发生一般性错误

    在PictureBox中有一副图,希望保存成图片,用 pictureBox1.Image("c:\\aa.jpg",System.Drawing.Imaging.ImageForm ...

  4. GDI+中发生一般性错误的解决办法 from http://www.cnblogs.com/winzheng/archive/2008/12/23/1360440.html...

    GDI+中发生一般性错误的解决办法 这个错误经常发生,代码如下:    private  static  byte[] GetBytes (Image image)         {         ...

  5. GDI+中发生一般性错误的解决办法(转帖)

    今天在开发.net引用程序中,需要System.Drawing.Image.Save 创建图片,debug的时候程序一切正常,可是发布到IIS后缺提示出现"GDI+中发生一般性错误" ...

  6. GDI+ 中发生一般性错误。

    GDI+ 中发生一般性错误. "/wechat"应用程序中的服务器错误. GDI+ 中发生一般性错误. 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息, ...

  7. GDI+中发生一般性错误的解决办法

    开发的过程中遇到了这个错误想要记录下来! 我是在保存图片文件时遇到的这个错误. 网上也搜了很多资料,试过之后还是会出错,代码其实也都大同小异没有太大区别,先把我遇到问题的经过描述一下: 我要做的是读取 ...

  8. GDI+ 中发生一般性错误(生成验证码时出现的错误)

    我在生成验证码时,出现这样的错误:GDI+ 中发生一般性错误. 怎么解决? 转载于:https://www.cnblogs.com/happyangle/archive/2008/06/11/1217 ...

  9. GDI+ 中发生一般性错误

    使用Chartlet时打开页面跳出以下错误: "/"应用程序中的服务器错误. GDI+ 中发生一般性错误. 说明: 执行当前 Web 请求期间,出现未处理的异常.请检查堆栈跟踪信息 ...

  10. GDI+中发生一般性错误 以及发布时候需要配置的文件

    mxcms在本地测试完毕.搬家到服务器上,修改测试数据新闻的时候,提示"GDI+中发生一般性错误". 找了下,发现是缩略图的原因. 解决方法:将缩略图文件夹里的文件全部删除. fi ...

最新文章

  1. 在Horizon Workspace中配置Windows单点登录-进阶篇
  2. 安装exchange server 2003服务器
  3. python入门与提高实践,Python基础06:功能增强与实践,基础知识,学习,函数,加强,及,练习...
  4. python redis 消息队列
  5. 「学习笔记——Python」Python 的模块(Modules)
  6. 大概看了一下《Flash MX 2004 -- 数据库应用开发 - 基于.NET架构》,感觉有点迷惘了!...
  7. Linux nano编辑txt文件,Linux 文本编辑器 nano 的简单使用
  8. 弹框在一个很的长页面居中显示
  9. php全选按钮怎么写,PHP中的“全选”复选框,其中包含header.php
  10. 第十七期:记一次生产环境SQL Server服务器卡顿问题解决--内存分配不当
  11. udp丢包解决办法 (没打开接收发送缓存)及setsockopt()用法 -转
  12. linux 压缩文件的命令总结
  13. 红旗服务器安装Tuxedo中间件
  14. Access入门之索引查询
  15. BIG5, GB(GB2312, GBK, ...), Unicode编码, UTF8, WideChar, MultiByte, Char说明与区别
  16. h5耳机线弯曲了怎么办_最烦人的耳机线,总是像麻花一样缠绕在一起,这是什么个道理...
  17. 职场 | 联发科MTK手机通信协议软件开发工程师面试总结
  18. 重复高斯勒让德法则(gauss-legendre)求积分(python,数值积分)
  19. 教你如何正确屏蔽掉WPS弹窗广告
  20. 彻底删除微软拼音输入法这个讨厌的家伙

热门文章

  1. map、mapPartitions、mapValues、mapWith、flatMap、flatMapWith、flatMapValues
  2. 非常有价值的电商系统,包括前台商城和后台管理系统!直接拿来用
  3. 首席架构师眼里的架构本质
  4. YouTube 架构揭秘与学习
  5. 小米网技术架构变迁实践
  6. vc浏览器_【36氪基金X一刻】零基础VC/PE行研标准班7月10日线上开课
  7. jenkins的安装与使用
  8. java通过smtp发送电子邮件
  9. Python内置函数(66)——vars
  10. 不用static,巧用对象.方法调用java中的函数