原文: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的互相转换的更多相关文章

  1. wpf 保存控件中的内容为图片格式

    黄色的是wpf控件的名称! //保存到特定路径            FileStream fs = new FileStream(@"C:\image.png", FileMod ...

  2. 异步方式向WPF ListBox控件中一条一条添加记录

    向ListBox绑定数据源时,如果数据量过大,可能会使得程序卡死,这是就需要一条一条的向ListBox的数据源中添加记录了,下面是个小Demo: 1.前台代码,就是一个ListBox控件 <Wi ...

  3. WPF DataGrid控件中某一列根据另一个文本列的值显示相应的模板控件

    之前做项目的时候需要实现这样一个功能.WPF DataGrid有两列,一列为"更新状态”列,一列为"值"列,如果"更新状态"列的值为“固定值更新”,则 ...

  4. WPF 列表控件中的子控件上下文绑定

    <DataGrid Grid.ColumnSpan=" Height="Auto" SelectedItem="{Binding Path=SelectP ...

  5. WPF TextBox控件中文字实现垂直居中

    TextBox纵向长度比较长但文字字体比较小的时候,在输入时就会发现文字不是垂直居中的. 而使用中我们发现,TextBox虽然可以设置文字的水平对齐方式,但却没有相应的属性让我们来调节他的垂直对齐方式 ...

  6. 创建 WPF 工具箱控件

    创建 WPF 工具箱控件 WPF (Windows Presentation Framework) 工具箱控件模板允许您创建 WPF 控件,会自动添加到 工具箱 安装扩展的安装. 本主题演示如何使用模 ...

  7. OpencvSharp 在WPF的Image控件中显示图像

    1.安装OpencvSharp 我使用的是VS2013 社区版,安装OpencvSharp3.0 在线安装方法:进入Tools,打开NuGet的包管理器 搜索Opencv 安装之后就可以使用,无需再做 ...

  8. WPF 使用依赖属性(DependencyProperty) 定义用户控件中的Image Source属性

    原文:WPF 使用依赖属性(DependencyProperty) 定义用户控件中的Image Source属性 如果你要自定义一个图片按钮控件,那么如何在主窗体绑定这个控件上图片的Source呢? ...

  9. 关于使用MVVM模式在WPF的DataGrid控件中实现ComboBox编辑列

    最近在做一个组态软件的项目,有一个需求需要在建立IO设备变量的时候选择变量的类型等. 建立IO变量的界面是一个DataGrid实现的,可以一行一行的新建变量,如下如所示: 这里需要使用带有ComboB ...

随机推荐

  1. android开发之PreferenceScreen使用详解

    是在惭愧,学习android也有一段时间了,今天才是第一次接触PreferenceScreen.记录下来,与大家分享. 本文参考:http://lovezhou.iteye.com/blog/1020 ...

  2. XC应用系列作品(Android应用)

    XC系列应用,如真题园手机客户端1.1等应用已经分别在 360手机助手.腾讯应用宝.百度手机助手.小米应用商店.豌豆荚.应用汇.木蚂蚁等安卓市场平台上线了! 本页面的系列应用是本人的开发的一Andro ...

  3. Java基础知识强化之网络编程笔记09:TCP之客户端键盘录入服务器写到文本文件中

    1. TCP之客户端键盘录入服务器写到文本文件中 (1)客户端: package cn.itcast_09; import java.io.BufferedReader; import java.io ...

  4. 如何获得Windows 8中已记住的WIFI的明文密码

    网上很流行的一种查看WIFI密码明文的方法,如下: 今天遇到了一种状况,就是如果不连WIFI的情况我能抓到这个密码吗?(实在不想开口问同事密码多少,只能苦逼的自己想办法了o(︶︿︶)o ) 答案当然是 ...

  5. shiro认证

    一.通过ini文件初始化一个用户 1.通过ini配置文件创建securityManager2.调用subject.login方法主体提交认证,提交的token3.securityManager进行认证 ...

  6. java常用正则表达式

    1.邮编 public static final String POSTAL_CODE = "^\\d{6}$"; 2. email(支持中文域名邮箱) 正则表达式  public ...

  7. Twisted介绍

    Twisted诞生于2000年初,作者为Glyph,目的是为了开发网络游戏. Twisted的历史 Glyph开始采用Java多线程,来开发Twisted Reality,结果多线程使得开发变得复杂, ...

  8. 浅谈iOS开发的协议(protocol)和代理(delegate)

    协议和代理对于一个新手来说确实不讨好理解,也有很多的iOS开发的老手对此是懂非懂的.网上的很多博文只是讲了怎么使用,并没有说的很明白.下面我谈一下我的理解. 1.你要先搞明白,协议和代理为什么会出现, ...

  9. IOS-UI- UIScrollView 滚动视图(1)

    滚动视图多个页面实现的原理 滚动视图位置不变 内容的位置发生改变. 滚动视图的运用1.分页查看图片 2.查看大图片 3.当内容过多需要一个页面显示,如:注册,修改个人信息等等4.当不希望用户感觉咱们的 ...

  10. 怎样在win7上远程连接linux系统

    window操作系统的电脑 一台安装了linux系统的服务器 putty.exe小软件 方法/步骤   在前面的环境和软件都有的情况下,双击putty.exe软件,如下图:   在软件界面中的:Hos ...