WPF Image控件中的ImageSource与Bitmap的互相转换
原文:WPF Image控件中的ImageSource与Bitmap的互相转换
1.从bitmap转换成ImageSource
[DllImport("gdi32.dll", SetLastError = true)] private static extern bool DeleteObject(IntPtr hObject); /// <summary> /// 从bitmap转换成ImageSource /// </summary> /// <param name="icon"></param> /// <returns></returns> public static ImageSource ChangeBitmapToImageSource(Bitmap bitmap) { //Bitmap bitmap = icon.ToBitmap(); IntPtr hBitmap = bitmap.GetHbitmap(); ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); if (!DeleteObject(hBitmap)) { throw new System.ComponentModel.Win32Exception(); } return wpfBitmap; }
2.从Bitmap转换成BitmapSource
/// <summary> /// 从Bitmap转换成BitmapSource /// </summary> /// <param name="bmp"></param> /// <returns></returns> public static BitmapSource ChangeBitmapToBitmapSource(this Bitmap bmp) { BitmapSource returnSource; try { returnSource = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(),IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } catch { returnSource = null; } return returnSource; }
3.从Icon到ImageSource的转换
/// <summary> /// 从Icon到ImageSource的转换 /// </summary> public ImageSource ChangeIconToImageSource( Icon icon) { ImageSource imageSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon( icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); return imageSource; }
4.从Icon到ImageSource的转换
internal static class IconUtilities
{
[DllImport("gdi32.dll", SetLastError = true)]
private static extern bool DeleteObject(IntPtr hObject); public static ImageSource ToImageSource(this Icon icon)
{
Bitmap bitmap = icon.ToBitmap();
IntPtr hBitmap = bitmap.GetHbitmap(); ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions()); if (!DeleteObject(hBitmap))
{
throw new Win32Exception();
} return wpfBitmap;
} // 这个是没有附加转换的,:)
public static ImageSource ToImageSource(this Icon icon)
{
ImageSource imageSource = Imaging.CreateBitmapSourceFromHIcon(
icon.Handle,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions()); return imageSource;
} }
调用:ImageSource wpfBitmap = img.ToImageSource();
参考:http://stackoverflow.com/questions/1127647/convert-system-drawing-icon-to-system-media-imagesource
5.从ImageSource转换成Bitmap,是从ImageSource中取出UriSource.LocalPath,然后使用 new Bitmap(FileName)的方法获取。其他的方法我还没有找到,
// System.Windows.Controls.Image ImgUserHeadFaceCutEdit; string str1 = ((BitmapImage)(ImgUserHeadFaceCutEdit.Source)).UriSource.AbsolutePath;// 此路径new Bitmap(str1)无法识别 string str2 = ((BitmapImage)(ImgUserHeadFaceCutEdit.Source)).UriSource.LocalPath ; //Bitmap sourceImage = new Bitmap(sourceImageUri.ToString()); string str3 = strImgSourceFileName; Console.WriteLine("AbsolutePath =" + str1); Console.WriteLine("LocalPath =" + str2); Console.WriteLine("srceFileName =" + str3);
这是运行结果:
AbsolutePath =C:/Documents%20and%20Settings/zp/%E6%A1%8C%E9%9D%A2/%E6%A1%8C%E9%9D%A2%E7%A7%80/10111411409225.jpg
LocalPath =C:\Documents and Settings\zp\桌面\桌面秀\10111411409225.jpg
srceFileName =C:\Documents and Settings\zp\桌面\桌面秀\10111411409225.jpg
谁找到了实现方法,留言下啊
转自:http://www.cnblogs.com/zp89850/archive/2011/10/27/2226039.html
WPF Image控件中的ImageSource与Bitmap的互相转换的更多相关文章
- wpf 保存控件中的内容为图片格式
黄色的是wpf控件的名称! //保存到特定路径 FileStream fs = new FileStream(@"C:\image.png", FileMod ...
- 异步方式向WPF ListBox控件中一条一条添加记录
向ListBox绑定数据源时,如果数据量过大,可能会使得程序卡死,这是就需要一条一条的向ListBox的数据源中添加记录了,下面是个小Demo: 1.前台代码,就是一个ListBox控件 <Wi ...
- WPF DataGrid控件中某一列根据另一个文本列的值显示相应的模板控件
之前做项目的时候需要实现这样一个功能.WPF DataGrid有两列,一列为"更新状态”列,一列为"值"列,如果"更新状态"列的值为“固定值更新”,则 ...
- WPF 列表控件中的子控件上下文绑定
<DataGrid Grid.ColumnSpan=" Height="Auto" SelectedItem="{Binding Path=SelectP ...
- WPF TextBox控件中文字实现垂直居中
TextBox纵向长度比较长但文字字体比较小的时候,在输入时就会发现文字不是垂直居中的. 而使用中我们发现,TextBox虽然可以设置文字的水平对齐方式,但却没有相应的属性让我们来调节他的垂直对齐方式 ...
- 创建 WPF 工具箱控件
创建 WPF 工具箱控件 WPF (Windows Presentation Framework) 工具箱控件模板允许您创建 WPF 控件,会自动添加到 工具箱 安装扩展的安装. 本主题演示如何使用模 ...
- OpencvSharp 在WPF的Image控件中显示图像
1.安装OpencvSharp 我使用的是VS2013 社区版,安装OpencvSharp3.0 在线安装方法:进入Tools,打开NuGet的包管理器 搜索Opencv 安装之后就可以使用,无需再做 ...
- WPF 使用依赖属性(DependencyProperty) 定义用户控件中的Image Source属性
原文:WPF 使用依赖属性(DependencyProperty) 定义用户控件中的Image Source属性 如果你要自定义一个图片按钮控件,那么如何在主窗体绑定这个控件上图片的Source呢? ...
- 关于使用MVVM模式在WPF的DataGrid控件中实现ComboBox编辑列
最近在做一个组态软件的项目,有一个需求需要在建立IO设备变量的时候选择变量的类型等. 建立IO变量的界面是一个DataGrid实现的,可以一行一行的新建变量,如下如所示: 这里需要使用带有ComboB ...
随机推荐
- CentOS7上GitHub/GitLab多帐号管理SSH Key
由于公司团队使用 GitLab 来托管代码,同时,个人在 Github 上还有一些代码仓库,可公司邮箱与个人邮箱是不同的,由此产生的 SSH key 也是不同的,这就造成了冲突 ,文章提供此类问题的解 ...
- MVC Ajax 提交是防止SCRF攻击
//在View中 <script type="text/javascript"> @functions{ public string ToKenHeaderValue( ...
- Linux只iptables
1. 查看<strong>网络</strong>监听的端口: netstat -tunlp 2. 查看本机的路由规则: route stack@ubuntu:~$ route ...
- javascript创建对象的7种方式
/*1.工厂模式*/ function createPerson(name,age,job) { var o = new object(); o.name = name; o.age = age; o ...
- SqlSugar常用增删改操作
一.添加数据 特别说明: 1.特别说明:对于自增长列的表插入数据后,当前自增长列的字段,仍旧为0,但可以通过Insert方法的返回值来获取 SqlSugarClient db = SugarConte ...
- python基础知识六
博客园的博文对每篇博文的长度似乎做了限制 面向对象编程, 在程序何种,根据操作数据的函数或语句块来设计程序.这被成为面向过程的编程.还有一种把数据和功能结合起来,用称为对象的东西包裹起来组织组织程序的 ...
- jsp中的注释
jsp中有各种针对不同类型语言的注释,值得注意的是对于标签 <jsp:include/>是需要使用jsp注释"<%----%>", (不能是<!-- ...
- <artifactId>maven-compiler-plugin</artifactId>
根据名字就可以看出来,就是控制编译环境的 <plugin> <groupId>org.apache.maven.plugins</groupId&g ...
- c# ADO连接Access 执行Open后程序自动退出
今天利用ADO连接Access数据库的时候遇到了前所未见的问题,Access数据库连接串,OleDbConnection,open的时候,系统就会自动关闭所有调试. 我就很纠结了,这个AccessHe ...
- Java学习----不该初始化的class(抽象类)
1. 抽象类声明有abstract 2.抽象类中有抽象方法,没有方法体的方法 // 抽象类 public abstract class Animal { public String name; pub ...