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 ...
随机推荐
- C#异步编程二
上一异步编程的博客还是在9月份的,最近事情也比较多,烦恼事情一个接着一个,一个人的周末除了无聊就剩无聊了,也只有写写博客来打发下这无聊的时光.原本想着尽快把异步编程这块总结一下,之前把委托异步算是总结 ...
- Windows8.1画热度图 - 坑
想要的效果 如上是silverlight版本.原理是设定一个调色板,为256的渐变色(存在一个png文件中,宽度为256,高度为1),然后针对要处理的距离矩阵图形,取图片中每个像素的Alpha值作为索 ...
- EmitMapper的使用
1.普通的映射. public class UserInfo { public int id { get; set; } public string name { get; set; } public ...
- CSS与JQuery的相关问题
文字隐藏:p div里面的文字过长时隐藏文字: overflow:hidden; text-overflow:ellipsis; white-space:nowrap; --------------- ...
- 天气预报API获取
1.citycode: http://mobile.weather.com.cn/js/citylist.xml http://files.cnblogs.com/files/ys-wuhan/cit ...
- jquery使用淘宝接口跨域查询手机号码归属地实例
<h1>手机号码归属地查询</h1> <div class="outer"> <p>请输入手机号码</p& ...
- centos中crontab(计时器)用法详解
关于crontab: crontab命令常见于Unix和类Unix的操作系统之中,用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行.该 ...
- 我的防Q+
Q+链接: http://onemore.web-45.com/index1.html: 兼容IE8: __页面被主机屋收回去了,现在又在弄自己的服务器,稍等呗
- 51nod 1060反素数
经典题. #include<map> #include<queue> #include<stack> #include<cmath> #include& ...
- 实现socket非阻塞设置
刚开始学习,难免有些疏漏之处,请各位多多指教. 程序亲测可以使用.废话不多说. 网络IO模型分为阻塞IO.非阻塞IO.异步IO.IO复用.信号驱动IO. 阻塞IO: 有数据时,直接读:没有数据时,等待 ...