GDI+中发生一般性错误 Winform Image.Save(mstream, ImageFormat.Png)引发
在处理图片时,读取本地图像文件,进行另存时发生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)引发相关推荐
- Image.Save()发生“GDI+ 中发生一般性错误”
从数据库中读取的图片是byte[]类型,将其转换成Image可以正常显示,但是调用image.Save()时会发生"GDI+ 中发生一般性错误". public static Sy ...
- C#界面设计--5--Bitmap.save保存图片时: GDI+ 中发生一般性错误 解决办法
Bitmap.save保存图片时: GDI+ 中发生一般性错误 解决办法 源程序: var date = DateTime.Now.ToString("yyyy-MM-dd");/ ...
- GDI+中发生一般性错误
在PictureBox中有一副图,希望保存成图片,用 pictureBox1.Image("c:\\aa.jpg",System.Drawing.Imaging.ImageForm ...
- GDI+中发生一般性错误的解决办法 from http://www.cnblogs.com/winzheng/archive/2008/12/23/1360440.html...
GDI+中发生一般性错误的解决办法 这个错误经常发生,代码如下: private static byte[] GetBytes (Image image) { ...
- GDI+中发生一般性错误的解决办法(转帖)
今天在开发.net引用程序中,需要System.Drawing.Image.Save 创建图片,debug的时候程序一切正常,可是发布到IIS后缺提示出现"GDI+中发生一般性错误" ...
- GDI+ 中发生一般性错误。
GDI+ 中发生一般性错误. "/wechat"应用程序中的服务器错误. GDI+ 中发生一般性错误. 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息, ...
- GDI+中发生一般性错误的解决办法
开发的过程中遇到了这个错误想要记录下来! 我是在保存图片文件时遇到的这个错误. 网上也搜了很多资料,试过之后还是会出错,代码其实也都大同小异没有太大区别,先把我遇到问题的经过描述一下: 我要做的是读取 ...
- GDI+ 中发生一般性错误(生成验证码时出现的错误)
我在生成验证码时,出现这样的错误:GDI+ 中发生一般性错误. 怎么解决? 转载于:https://www.cnblogs.com/happyangle/archive/2008/06/11/1217 ...
- GDI+ 中发生一般性错误
使用Chartlet时打开页面跳出以下错误: "/"应用程序中的服务器错误. GDI+ 中发生一般性错误. 说明: 执行当前 Web 请求期间,出现未处理的异常.请检查堆栈跟踪信息 ...
- GDI+中发生一般性错误 以及发布时候需要配置的文件
mxcms在本地测试完毕.搬家到服务器上,修改测试数据新闻的时候,提示"GDI+中发生一般性错误". 找了下,发现是缩略图的原因. 解决方法:将缩略图文件夹里的文件全部删除. fi ...
最新文章
- 在Horizon Workspace中配置Windows单点登录-进阶篇
- 安装exchange server 2003服务器
- python入门与提高实践,Python基础06:功能增强与实践,基础知识,学习,函数,加强,及,练习...
- python redis 消息队列
- 「学习笔记——Python」Python 的模块(Modules)
- 大概看了一下《Flash MX 2004 -- 数据库应用开发 - 基于.NET架构》,感觉有点迷惘了!...
- Linux nano编辑txt文件,Linux 文本编辑器 nano 的简单使用
- 弹框在一个很的长页面居中显示
- php全选按钮怎么写,PHP中的“全选”复选框,其中包含header.php
- 第十七期:记一次生产环境SQL Server服务器卡顿问题解决--内存分配不当
- udp丢包解决办法 (没打开接收发送缓存)及setsockopt()用法 -转
- linux 压缩文件的命令总结
- 红旗服务器安装Tuxedo中间件
- Access入门之索引查询
- BIG5, GB(GB2312, GBK, ...), Unicode编码, UTF8, WideChar, MultiByte, Char说明与区别
- h5耳机线弯曲了怎么办_最烦人的耳机线,总是像麻花一样缠绕在一起,这是什么个道理...
- 职场 | 联发科MTK手机通信协议软件开发工程师面试总结
- 重复高斯勒让德法则(gauss-legendre)求积分(python,数值积分)
- 教你如何正确屏蔽掉WPS弹窗广告
- 彻底删除微软拼音输入法这个讨厌的家伙