2013/09/17 C# WPF 学习笔记 图片转换

来源:互联网 发布:马里兰艺术学院 知乎 编辑:程序博客网 时间:2024/06/05 19:17

Convert System.Drawing.Icon to System.Media.ImageSource (将System.Drawing.Icon 转成 System.Media.ImageSource)

WPF

Properties [Resources.resx]  资源

引入 *.Icon文件时 资源为System.Drawing.Icon

TreeView 自定义结点 有Image元素 需要绑定 Source => System.Media.ImageSource

所以需要 将System.Drawing.Icon 转成 System.Media.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;    }}


转载自(http://stackoverflow.com/questions/1127647/convert-system-drawing-icon-to-system-media-imagesource)

原创粉丝点击