这个错误经常发生,代码如下:

  1. private  static  byte[] GetBytes (Image image)
  2. {
  3. try
  4. {
  5. if (image == null) return null;
  6. using (MemoryStream stream = new MemoryStream())
  7. {
  8. image .Save(stream, ImageFormat.Jpeg);
  9. return stream.GetBuffer();
  10. }
  11. }
  12. finally
  13. {
  14. if(image != null)
  15. {
  16. image.Dispose();
  17. image = null;
  18. }
  19. }
  20. }

修改后的代码如下:

  1. private  static  byte[] GetBytes (Image image)
  2. {
  3. try
  4. {
  5. if (image == null) return null;
  6. using(Bitmap bitmap = new Bitmap(image))
  7. {
  8. using (MemoryStream stream = new MemoryStream())
  9. {
  10. bitmap.Save(stream, ImageFormat.Jpeg);
  11. return stream.GetBuffer();
  12. }
  13. }
  14. }
  15. finally
  16. {
  17. if(image != null)
  18. {
  19. image.Dispose();
  20. image = null;
  21. }
  22. }
  23. }

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() 是首 位图 处置。

创建非索引图像,例如:

  1. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  2. {
  3. //创建一个bitmap类型的bmp变量来读取文件。
  4. Bitmap bmp = new Bitmap(openFileDialog1 .FileName );
  5. //新建第二个bitmap类型的bmp2变量,我这里是根据我的程序需要设置的。
  6. Bitmap bmp2 = new Bitmap(1024, 768, PixelFormat.Format16bppRgb555);
  7. //将第一个bmp拷贝到bmp2中
  8. Graphics draw = Graphics.FromImage(bmp2);
  9. draw.DrawImage(bmp,0,0);
  10. pictureBox1.Image = (Image)bmp2 ;//读取bmp2到picturebox
  11. FILE = openFileDialog1.FileName;
  12. openFileDialog1.Dispose();
  13. draw.Dispose();
  14. bmp.Dispose();//释放bmp文件资源
  15. }

如果是在Web 程序中注意这些设置:

1. 相应的帐户没有写权限。
解决方法:赋予 NETWORK SERVICE 帐户以写权限。
2. 指定的物理路径不存在。
解决方法:
在调用 Save 方法之前,先判断目录是否存在,若不存在,则创建。
if (!Directory.Exists(dirpath))
Directory.CreateDirectory(dirpath);

最新文章

  1. NoSQL初探之人人都爱Redis:(3)使用Redis作为消息队列服务场景应用案例
  2. 坑爹坑娘坑祖宗的87端口(记一次tomcat故障排查)
  3. LINUX安全加固规范
  4. HR开发 SuccessFactors与HCM数据映射
  5. oracle 第一章总结
  6. Vim 的 tab 设置
  7. Convert part to feature command
  8. 设置ORACLE数据库游标大小
  9. Sina App Engine(SAE)入门教程(4)- SaeVCode(验证码服务)使用
  10. MVC架构和SSH框架对应关系
  11. 8-7-Exercise
  12. Codeforces#313
  13. 使用OpenSSL生成证书
  14. fragment 中利用spinner实现省市联动
  15. 新秀nginx源代码分析数据结构篇(四)红黑树ngx_rbtree_t
  16. 新更新,又是一年了。这次记录下关于android版的WeiboDemo的问题
  17. Linux下查看tomcat版本
  18. scrapy meta不用pipe用命令-o
  19. linux windows安装python的最佳方式,miniconda
  20. mysql实践总结

热门文章

  1. jQuery中的width() innerWidth() outerWidth() outerWidth(true)的区别
  2. iOS 2D绘图 (Quartz 2D) 概述
  3. C#调用LUA函数
  4. bzoj 2287: 【POJ Challenge】消失之物
  5. 【跟着子迟品 underscore】JavaScript 数组展开以及重要的内部方法 flatten
  6. 【原】关于使用sklearn进行数据预处理 —— 归一化/标准化/正则化
  7. java-工具类-读取配置文件
  8. Java中使用Jedis操作Redis(转载)
  9. python 中文乱码问题2
  10. Leetcode 254. Factor Combinations