原文: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. [转] Putty - 文件夹显示的蓝色太暗

    Putty SSH key的后缀为ppk 默认文件夹的颜色显示为ANSI Blue,颜色太暗. ANSI Blue : RGB(0, 0, 187) 将ANSI Blue修改为和ANSI Blue B ...

  2. 经典关于多态的demo

    class Foo { public int a; public Foo() { a = 3; } public int addFive() { a += 5; return a; } public ...

  3. Java SE (2)之 Graphics 画图工具

    Graphics 绘图类: 提供两个方法.Paint (绘图,被系统自动调用)    repaint(重绘) Paint 调用原理(1.窗口最大化,再最小化 窗口的大小发生变化 Repaint函数被调 ...

  4. [视频转换] C#VideoConvert视频转换帮助类 (转载)

    点击下载 VideoConvert.zip 主要功能如下 .获取文件的名字 .获取文件扩展名 .获取文件类型 .视频格式转为Flv .生成Flv视频的缩略图 .转换文件并保存在指定文件夹下 .转换文件 ...

  5. [DEncrypt] Encrypt--加密/解密/MD5加密 (转载)

    点击下载  Encrypt.zip 这个类是关于加密,解密的操作,文件的一些高级操作1.Encrypt加密2.Encrypt解密3.Encrypt MD5加密看下面代码吧 /// <summar ...

  6. 从source folder 下将其所有子文件夹的*.* 文件拷贝到 target folder (不拷贝文件夹名仅拷贝文件)

    因本人较懒,一直认为电脑能做的就让电脑来做,所以写下这个批处理的小脚本方便工作. 场景:碰到要拷贝一个文件夹(source folder)下的多个子文件夹(sub-folder)的文件到指定文件夹下( ...

  7. 查看Safari和钥匙串中的密码

    Safari Safari的同步书签功能很棒,还可以看到其他设备没关掉的网页.为了省时间,一些经常进的网站,比如博客,邮箱等,我都会选择让Safari保存密码,还使用iCloud同步!因为一直很放心苹 ...

  8. C++重载流插入运算符和流提取运算符【转】

    C++的流插入运算符“<<”和流提取运算符“>>”是C++在类库中提供的,所有C++编译系统都在类库中提供输入流类istream和输出流类ostream.cin和cout分别是 ...

  9. sublime text3-代码片段配置

    1.Tools->New Snippet-> <snippet>     <content><![CDATA[${1:public }function ${2 ...

  10. php开源项目学习二次开发的计划

      开源项目: cms 国内 dedecms cmstop 国外 joomla, drupal 电商 国内 ecshop 国外 Magento 论坛 discuz 博客 wordpress   学习时 ...