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 ...
随机推荐
- lecture5-对象识别与卷积神经网络
Hinton第五课 突然不知道object recognition 该翻译成对象识别好,还是目标识别好,还是物体识别好,但是鉴于范围性,还是翻译成对象识别吧.这一课附带了两个论文<Convolu ...
- Android一键多渠道分发打包实战和解析
当项目需要有更多的客户的时候,你就会考虑将apk上架到应用商店了,无奈天朝Android应用商店真的是百家争鸣,据某地不完全统计已经有900+.若将Apk上架到所有的应用商店是个好主意,但是据统计也就 ...
- java并发:线程同步机制之Volatile关键字&原子操作Atomic
volatile关键字 volatile是一个特殊的修饰符,只有成员变量才能使用它,与Synchronized及ReentrantLock等提供的互斥相比,Synchronized保证了Synchro ...
- ASP.NET MVC 多语言实现技巧 最简、最易维护和最快速开发
说说传统做法的缺点 1.做过多语言的都知道这玩意儿太花时间 2.多语言架构一般使用资源文件.XML或者存储数据库来实现.这样就在一定程序上降低了性能 3.页面的可读性变差,需要和资源文件进行来回切换 ...
- 【技术】JavaSE环境下JPA实体类自动注册
在没有容器支持的环境下,JPA的实体类(Entity)一般要在persistence.xml中逐个注册,类似下面这样: <?xml version="1.0" encodin ...
- Learning to Rank 之 listwise ranking
详细文章: http://www.machinelearning.org/proceedings/icml2007/papers/139.pdf
- 解决"415 Cannot process the message because the content type 'application/x-www-form-urlencoded' was not the expected type 'text/xml; charset=utf-8'"
wcf basicHttpBinding content-type text/xml;charset=utf-8 wsHttpBinding 'application/soap+xml; ch ...
- 1031MVCC和事务浅析
转自 http://blog.csdn.net/sofia1217/article/details/50778906 关于MVCC浅析,有些难度http://xuebinbin212.blog.163 ...
- PHP自学链接收藏
PHP之道 laravist Sublime Text 3
- 编写实现连接oracle数据库并返回Connection对象的Java工具类
只需要实现一个功能,所以只写一个方法,为了方便调用,设为静态方法 package com.jv; import java.sql.Connection; import java.sql.DriverM ...