GDI+中发生一般性错误的解决办法
这个错误经常发生,代码如下:
- private static byte[] GetBytes (Image image)
- {
- try
- {
- if (image == null) return null;
- using (MemoryStream stream = new MemoryStream())
- {
- image .Save(stream, ImageFormat.Jpeg);
- return stream.GetBuffer();
- }
- }
- finally
- {
- if(image != null)
- {
- image.Dispose();
- image = null;
- }
- }
- }
修改后的代码如下:
- private static byte[] GetBytes (Image image)
- {
- try
- {
- if (image == null) return null;
- using(Bitmap bitmap = new Bitmap(image))
- {
- using (MemoryStream stream = new MemoryStream())
- {
- bitmap.Save(stream, ImageFormat.Jpeg);
- return stream.GetBuffer();
- }
- }
- }
- finally
- {
- if(image != null)
- {
- image.Dispose();
- image = null;
- }
- }
- }
MSDN 解释如下:
Bitmap 对象或一个 图像 对象从一个文件, 构造时该文件仍保留锁定对于对象的生存期。 因此, 无法更改图像并将其保存回它产生相同的文件。
替代方法
• 创建非索引映像。
• 创建索引映像。
这两种情况下, 原始 位图 上调用 Bitmap.Dispose() 方法删除该文件上锁或删除要求, 流或内存保持活动。
创建非索引图像
即使原始映像被索引格式中该方法要求新图像位于每像素 (超过 8 位 -) -, 非索引像素格式。 此变通方法使用 Graphics.DrawImage() 方法来将映像复制到新 位图 对象:
1. 构造从流、 从内存, 或从文件原始 位图 。
2. 创建新 位图 的相同大小, 带有是超过 8 位 - - 像素 (BPP) 每像素格式。
3. 使用 Graphics.FromImage() 方法以获取有关二 位图 Graphics 对象。
4. 用于 Graphics.DrawImage() 绘制首 位图 到二 位图 。
5. 用于 Graphics.Dispose() 处置是 图形 。
6. 用于 Bitmap.Dispose() 是首 位图 处置。
创建索引映像
此解决办法在索引格式创建一个 Bitmap 对象:
1. 构造从流、 从内存, 或从文件原始 位图 。
2. 创建新 位图 具有相同的大小和像素格式作为首 位图 。
3. 使用 Bitmap.LockBits() 方法来锁定整个图像对于两 Bitmap 对象以其本机像素格式。
4. 使用 Marshal.Copy 函数或其他内存复制函数来从首 位图 复制到二 位图 图像位。
5. 使用 Bitmap.UnlockBits() 方法可以解锁两 Bitmap 对象。
6. 用于 Bitmap.Dispose() 是首 位图 处置。
创建非索引图像,例如:
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- //创建一个bitmap类型的bmp变量来读取文件。
- Bitmap bmp = new Bitmap(openFileDialog1 .FileName );
- //新建第二个bitmap类型的bmp2变量,我这里是根据我的程序需要设置的。
- Bitmap bmp2 = new Bitmap(1024, 768, PixelFormat.Format16bppRgb555);
- //将第一个bmp拷贝到bmp2中
- Graphics draw = Graphics.FromImage(bmp2);
- draw.DrawImage(bmp,0,0);
- pictureBox1.Image = (Image)bmp2 ;//读取bmp2到picturebox
- FILE = openFileDialog1.FileName;
- openFileDialog1.Dispose();
- draw.Dispose();
- bmp.Dispose();//释放bmp文件资源
- }
如果是在Web 程序中注意这些设置:
1. 相应的帐户没有写权限。
解决方法:赋予 NETWORK SERVICE 帐户以写权限。
2. 指定的物理路径不存在。
解决方法:
在调用 Save 方法之前,先判断目录是否存在,若不存在,则创建。
if (!Directory.Exists(dirpath))
Directory.CreateDirectory(dirpath);
最新文章
- NoSQL初探之人人都爱Redis:(3)使用Redis作为消息队列服务场景应用案例
- 坑爹坑娘坑祖宗的87端口(记一次tomcat故障排查)
- LINUX安全加固规范
- HR开发 SuccessFactors与HCM数据映射
- oracle 第一章总结
- Vim 的 tab 设置
- Convert part to feature command
- 设置ORACLE数据库游标大小
- Sina App Engine(SAE)入门教程(4)- SaeVCode(验证码服务)使用
- MVC架构和SSH框架对应关系
- 8-7-Exercise
- Codeforces#313
- 使用OpenSSL生成证书
- fragment 中利用spinner实现省市联动
- 新秀nginx源代码分析数据结构篇(四)红黑树ngx_rbtree_t
- 新更新,又是一年了。这次记录下关于android版的WeiboDemo的问题
- Linux下查看tomcat版本
- scrapy meta不用pipe用命令-o
- linux windows安装python的最佳方式,miniconda
- mysql实践总结
热门文章
- jQuery中的width() innerWidth() outerWidth() outerWidth(true)的区别
- iOS 2D绘图 (Quartz 2D) 概述
- C#调用LUA函数
- bzoj 2287: 【POJ Challenge】消失之物
- 【跟着子迟品 underscore】JavaScript 数组展开以及重要的内部方法 flatten
- 【原】关于使用sklearn进行数据预处理 —— 归一化/标准化/正则化
- java-工具类-读取配置文件
- Java中使用Jedis操作Redis(转载)
- python 中文乱码问题2
- Leetcode 254. Factor Combinations