分析:

一般出现这种问题都是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+的更多相关文章

  1. GDI+一般性错误(A generic error occurred in GDI+)

    1.GDI+的前世今生 GDI+全称图形设备接口,Graphics Device Interface (GDI) ,他的爸爸叫做GDI, 用C写的.Windows XP出来以后用C++重新写了一下,变 ...

  2. A generic error occurred in GDI+的解决方案

    转自智慧光原文A generic error occurred in GDI+. 解决方法 使用image1.RotateFlip(RotateFlipType.Rotate90FlipNone)方法 ...

  3. C# GDI+发生一般性错误(A generic error occurred in GDI+))

    解决思路: 1. 因为 .net GDI+ 是对底层 的封装. 所以可以尝试用 Marshal.GetLastWin32Error();函数获得底层错误代码. try{ image.Save(file ...

  4. A generic error occurred in GDI+. 上传图片报错

    代码就不说了,因为本地测试 ok, 服务端 就不行 ,服务器 环境 阿里云 win2008 r2  64 位 原因 是我没有这是 文件加权限 : 左边 的 少了 权限~ 代码 :含义是 网络图片 裁剪 ...

  5. 启动Eclipse发生错误:An internal error occurred during: "Initializing Java Tooling".

    问题描述   由于上一次关闭 Eclipse 时没有正常关闭,再次启动 Eclipse 时报错:An internal error occurred during: "Initializin ...

  6. 解决: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 ...

  7. 创建Maven项目出现:An internal error occurred during: "Retrieving archetypes:". Java heap space 错误解决办法

    首先说明一下网上的方法: 在Eclipse中创建Maven的Web项目时出现错误:An internal error occurred during: "Retrieving archety ...

  8. Eclipse启动发生的错误:An internal error occurred during: "Initializing Java Tooling".

    1.启动Eclipse时,初始化异常:An internal error occurred during: "Initializing Java Tooling". 解决方案:wi ...

  9. 禁止root远程登录 sshd问题:A protocol error occurred. Change of username or service not allowed

    在研究Linux安全的时候遇到一个问题,原本打算修改linux直接远程root登陆,修改为sshd的配置文件后 Nano /etc/ssh/sshd_config 把#PermitRootLogin ...

随机推荐

  1. 【讲义提纲】以一个实战新闻cms增删改查demo为例,给学院国创队伍培训php

    PHP实战基础——以一个新闻cms的增删改查为例 一.        环境配置 二.        数据库创建 三.        增删改查demo 连接数据库 <?php $link=mysq ...

  2. 异步dcfifo的读写

    异步dcfifo的原理 Dcfifo即是Double clk fifo,意思是双时钟的fifo.或许你现在还不知道什么是fifo,那我就先从fifo(就是同步fifo,不过同步fifo在实际运用中比较 ...

  3. The median of multi ascending array

    Given 17 arrays,every array is ascending.The task is to get the median of all these numbers. 0 1 2 3 ...

  4. 【心得】怪异的JS的Date函数

    我们知道new Date('2013/1/1')是2013年1月1日, 那么new Date('2013/1/366')会报无效日期格式吗?答案是,这是一个有效的日期,但是他是表示2014年1月1日. ...

  5. 1014mysqldumpslow.pl简单分析慢日志 WINDOW平台

    转自http://www.th7.cn/db/mysql/201507/113998.shtml 要想运行mysqldumpslow.pl(这是perl程序),下载perl编译器.下载地址:http: ...

  6. 第八章:Java集合

    1.Java集合 A:对象的容器. B:实现数据结构(栈.队列) 2.  Set:无序不重复 List: 有序可重复,长度可变. Map: 存放键值对. 3.  Iterator foreach

  7. 网络设计中需要考虑的时延latency差异

    Jeff Dean提到不同数据访问方式latency差异 Numbers Everyone Should Know L1 cache reference 0.5 ns Branch mispredic ...

  8. Java--笔记(2)

    11.根据操作系统中的定义:死锁是指在一组进程中的各个进程均占有不会释放的资源,但因互相申请被其他进程所站用不会释放的资源而处于的一种永久等待状态. 死锁的四个必要条件: 1)互斥条件(Mutual ...

  9. 使用jmeter进行性能测试-Jmeter教程及技巧汇总 (转)

    http://www.jmeter.cf/loadtesting-jmeter.html 为什么使用jmeter, 它免费开源, 不断发展, 功能逐渐强大. 可以做功能,负载, 性能测试.一套脚本可以 ...

  10. selenium+eclispse里代码备注

    1.火狐.谷歌和IE浏览器引擎都要重新下载selenium官网引擎,并设置路径才可以支持selenium3 而狐火用自己的引擎不用设置路径既可以支持selenium2也支持selenium3,谷歌和I ...