Exception:A generic error occurred in GDI+
分析:
一般出现这种问题都是GDI和原数据(比如Bitmap)是同一个实体,只不过是两个引用。换句话说就是这个路径的图片被GDI占用啦。
还有一种情况是路径有问题。
场景一:
WPF的Image控件的Source属性绑定一个图片路径的时候需要吧Bitmap转换成ImageSource。
Bitmap _lastBitmap;
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{ if (value != null && System.IO.File.Exists(value.ToString()))
{
if (_lastBitmap != null)
_lastBitmap.Dispose(); var path = value.ToString();
Bitmap bmp = (Bitmap)Bitmap.FromFile(path);
_lastBitmap = new Bitmap(bmp.Width, bmp.Height);
using (Graphics g = Graphics.FromImage(_lastBitmap))
{
g.DrawImageUnscaled(bmp, , );
}
bmp.Dispose();
return PublicMethod.BitMapToImageSource(_lastBitmap);
}
return null;
}
public static ImageSource BitMapToImageSource(Bitmap bmp)
{
BitmapSource returnSource;
try
{
returnSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
catch
{
returnSource = null;
}
return returnSource;
}
在Convert的方法里面用到啦Graphics,就是为啦吧图片这个原数据分成两个实体,一个为GDI显示,一个为以后别的操作而用,比如:
保存相同路径的不同图片,需要通知界面更新重新绑定Path对应的图片。Bitmap.Save(“Path”).
场景二:
比如保存的路径为"D:\ my_testfiles \hello.png",中间的目录名字两边存在空格。在实际创建目录的时候是不被允许两边有空格的,但是自己拼接路径的时候难免在两边多个空格。
Exception:A generic error occurred in GDI+的更多相关文章
- GDI+一般性错误(A generic error occurred in GDI+)
1.GDI+的前世今生 GDI+全称图形设备接口,Graphics Device Interface (GDI) ,他的爸爸叫做GDI, 用C写的.Windows XP出来以后用C++重新写了一下,变 ...
- A generic error occurred in GDI+的解决方案
转自智慧光原文A generic error occurred in GDI+. 解决方法 使用image1.RotateFlip(RotateFlipType.Rotate90FlipNone)方法 ...
- C# GDI+发生一般性错误(A generic error occurred in GDI+))
解决思路: 1. 因为 .net GDI+ 是对底层 的封装. 所以可以尝试用 Marshal.GetLastWin32Error();函数获得底层错误代码. try{ image.Save(file ...
- A generic error occurred in GDI+. 上传图片报错
代码就不说了,因为本地测试 ok, 服务端 就不行 ,服务器 环境 阿里云 win2008 r2 64 位 原因 是我没有这是 文件加权限 : 左边 的 少了 权限~ 代码 :含义是 网络图片 裁剪 ...
- 启动Eclipse发生错误:An internal error occurred during: "Initializing Java Tooling".
问题描述 由于上一次关闭 Eclipse 时没有正常关闭,再次启动 Eclipse 时报错:An internal error occurred during: "Initializin ...
- 解决:An internal error occurred during: "Launching New_configuration". Path for project must have only one segment.
问题: 点击运行时eclipse报错如下: An internal error occurred during: "Launching New_configuration". Pa ...
- 创建Maven项目出现:An internal error occurred during: "Retrieving archetypes:". Java heap space 错误解决办法
首先说明一下网上的方法: 在Eclipse中创建Maven的Web项目时出现错误:An internal error occurred during: "Retrieving archety ...
- Eclipse启动发生的错误:An internal error occurred during: "Initializing Java Tooling".
1.启动Eclipse时,初始化异常:An internal error occurred during: "Initializing Java Tooling". 解决方案:wi ...
- 禁止root远程登录 sshd问题:A protocol error occurred. Change of username or service not allowed
在研究Linux安全的时候遇到一个问题,原本打算修改linux直接远程root登陆,修改为sshd的配置文件后 Nano /etc/ssh/sshd_config 把#PermitRootLogin ...
随机推荐
- 采访ServiceStack的项目领导Demis Bellot——第1部分(网摘)
ServiceStack是一个开源的.支持.NET与Mono平台的REST Web Services框架.InfoQ有幸与Demis Bellot深入地讨论了这个项目.在这篇两部分报道的第1部分中,我 ...
- android listview 的监听事件
今天遇到了一个比较让我头疼的问题,不过追根揭底只是我对listview理解的不够透彻罢了, 闲言少叙,说说我遇到的问题吧: 上篇随笔我写了关于listview的使用,如果你也已经写好了列表那么恭喜这一 ...
- Beta项目冲刺 --第一天
新的开始.. 队伍:F4 成员:031302301 毕容甲 031302302 蔡逸轩 031302430 肖阳 031302418 黄彦宁 会议内容: 1.站立式会议照片: 2.项目燃尽图 3.冲刺 ...
- 使用Servlet实现下载文件的功能
在前台有一个下载链接,比如 <a href="DownLoadServlet">下载</a> <br/> 使用Servlet实现下载: impo ...
- [转]SVN版本冲突解决详解
原文地址:http://blog.csdn.net/windone0109/article/details/4857044 版权声明:本文为博主原创文章,未经博主允许不得转载. 版本冲突原因: 假设A ...
- 【BZOJ 4456】【UOJ #184】【ZJOI 2016】旅行者
http://www.lydsy.com/JudgeOnline/problem.php?id=4456 http://uoj.ac/problem/184 参考(抄)的晨爷的题解(代码) 对矩形进行 ...
- 史上最全最强SpringMVC详细示例实战教程
一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 ...
- Bootstrap表单布局样式
1.并排和下拉选项 <form class="form-horizontal" role="form"> <fieldset> < ...
- 使用 Daynamic 动态添加属性
所谓的Dynamic 动态类型,页面也要是动态的(强撸) 很简单的 直接上代码: //案例一 DynamicpersonCollection = new ObservableCollection(); ...
- Web前端性能优化教程03:添加Expires头
本文是Web前端性能优化系列文章中的第三篇,主要讲述添内容:加Expires头.完整教程可查看:Web前端性能优化 什么是Expires头? Expires存储的是一个用来控制缓存失效的日期.当浏览器 ...