分析:

一般出现这种问题都是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, 0, 0);}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",中间的目录名字两边存在空格。在实际创建目录的时候是不被允许两边有空格的,但是自己拼接路径的时候难免在两边多个空格。

转载于:https://www.cnblogs.com/kissfu/p/3422829.html

Exception:A generic error occurred in GDI+相关推荐

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

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

  2. eclipse突然报错:An internal error occurred during: “Compute launch button tooltip“.并且运行main方法时Run As后面是空

    一.报错信息: An internal error occurred during: "Compute launch button tooltip". org.springfram ...

  3. 安装Tensorflow解决:An HTTP error occurred when trying to retrieve this URL.

    目录 问题描述 问题原因 解决方法 问题描述 在Anacoda Prompt中采用命令安装pip install tensorflow,提示: An HTTP error occurred when ...

  4. MAVEN项目报错:An internal error occurred during: Updating Maven Project java.lang.NullPoint

    MAVEN项目报错:An internal error occurred during: "Updating Maven Project" java.lang.NullPoint ...

  5. Adobe Acrobat DC无法生成pdf:an unexpected error occurred pdfmaker was unable to produce the adobe pdf

    在分享笔者解决这个问题的经验之前,首先说一下笔者的结论: 在排除Adobe Acrobat自身软件原因之后,若出现an unexpected error occurred pdfmaker was u ...

  6. 程序运行报错:A JavaScript error occurred in the main process

    找到电脑中的%appdata%文件 在文件夹中,找当出错软件的缓存文件夹, 这里假设出错的是baidu 删除,重启软件,还不行的话,删除软件,然后删除日志文件,再重启电脑,重装软件. 出现A Java ...

  7. Django - 网页加载报错:A server error occurred. Please contact the administrator(亲测)

    1.网页加载时报错显示: 2.pycharm中Terminal报错显示: 如: File "E:\flask_env\django_env\lib\site-packages\django\ ...

  8. 解决:An HTTP error occurred when trying to retrieve this URL.

    一.问题描述 使用Anaconda下载东西时出现的错误.分析原因是:默认使用国外的源下载,出现错误:解决方案是:使用国内的镜像源. 二.问题解决 使用清华镜像源:https://mirrors.tun ...

  9. yarn安装插件报错: An unexpected error occurred: “https://registry.npmjs.org/vue-router: connect ETIMEDOUT

    在安装vue-router的时候,终端报下面的错误 解决方法==>终端输入以下指令:yarn config set sass-binary-site http://npm.taobao.org/ ...

  10. 解决方法:A fatal error occurred. The required library hostfxr.dll could not be found.

    今天在将C#应用程序在另一台电脑上运行时发生标题所示错误 查了一下这应该是VS版本升级导致的问题,原本的应用程序实在VS2019上生成的,没有问题,后来在另外电脑上运行,命令窗口出现这个错误 看了一下 ...

最新文章

  1. 删除数据 DataIntegrityViolationException: not-null property references a null or transient value解决...
  2. Py之PIL:不一样的PS之利用PIL库的img.paste方法实现合成刘若英导演电影《后来的我们》海报设计
  3. How does asp.net web api work?
  4. GStreamer 的调试工具
  5. c语言1000内亲密对数,《C语言程序的设计上机指导》项目五函数及其应用.pptx
  6. caffe 提取特征并可视化(已测试可执行)及在线可视化
  7. 【Python基础知识-pycharm版】第五节-字典\集合
  8. 切割照片php上传,php下ajax的文件切割上传
  9. redis热点key解决方案_缓存穿透,缓存雪崩,4种解决方案分析
  10. 学习笔记:可持久化线段树(主席树):静态 + 动态
  11. ltrim函数_数据分析常用Excel函数
  12. (二)Luence——代码实现索引及搜索
  13. ffmpeg文档4:创建线程
  14. passing 'const xxx ' as 'this' argument discards qualifiers [-fpermissive]
  15. 数据分析师python 城市数据团_城市数据分析师
  16. BAT、360、今日头条的小程序大战 谁会是最后的赢家?
  17. Android 免 root 抓包简易教程
  18. 数仓学习笔记(4)——数仓搭建(ODS层和DIM层)
  19. 9、图解门禁系统电锁分类及安装方法
  20. 全国二级计算机c语言试题及答案,全国计算机二级《C语言》预测试题及答案2016...

热门文章

  1. 页面转发后文本显示???_无代码软件开发中超文本显示设计
  2. Tensorflow笔记:神经网络优化
  3. HDU-1159-Common Subsequence
  4. [LintCode] 翻转二叉树
  5. window下搭建qt开发环境编译、引用ace
  6. 微软2012年突破重围 主攻Windows Phone
  7. ArrayList常用函数
  8. C++--第22课 - 类模板 - 下
  9. 多线程中使用CheckForIllegalCrossThreadCalls = false访问窗口
  10. Flex布局使用总结