Bitmap 与ImageSource之间的转换
public class ImageConverter
{
[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)
{
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;
}
/// <summary>
/// 从Bitmap转换成BitmapSource
/// </summary>
/// <param name="bmp"></param>
/// <returns></returns>
public static BitmapSource ChangeBitmapToBitmapSource(Bitmap bmp)
{
BitmapSource returnSource;
try
{
returnSource = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
catch
{
returnSource = null;
}
return returnSource;
}
/// <summary>
/// 从Icon到ImageSource的转换
/// </summary>
public ImageSource ChangeIconToImageSource(Icon icon)
{
ImageSource imageSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
icon.Handle,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
return imageSource;
}
}
Bitmap 与ImageSource之间的转换的更多相关文章
- Android图片二进制与Bitmap、Drawable之间的转换
Android图片二进制与Bitmap.Drawable之间的转换 Java代码 public byte[] getBitmapByte(Bitmap bitmap){ ByteArray ...
- Drawable、Bitmap、byte[]之间的转换
android在处理一写图片资源的时候,会进行一些类型的转换: 1 Drawable → Bitmap 的简单方法 ((BitmapDrawable)res.getDrawable(R.drawabl ...
- C# 图像处理:Bitmap 与 Image 之间的转换
Image img = this.pictureBox1.Image; Bitmap map = new Bitmap(img); Image img = Bitmap; Image和Bitmap类概 ...
- Bitmap与String之间的转换
/** * 将bitmap转换成base64字符串 * * @param bitmap * @return base64 字符串 */ public String bitmaptoString(Bit ...
- Android Drawable、Bitmap、byte[]之间的转换
转自http://blog.csdn.net/june5253/article/details/7826597 1.Bitmap-->Drawable Bitmap drawable2Bitma ...
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
package com.soai.imdemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; ...
- 简单谈谈Resource,Drawable和Bitmap之间的转换
一直接触这些东西,还是归个类整理一下比较好. Resource -> Drawable Drawable draw1 = this.getResources().getDrawable(R.dr ...
- 【C#/WPF】Bitmap、BitmapImage、ImageSource 、byte[]转换问题
C#/WPF项目中,用到图像相关的功能时,涉及到多种图像数据类型的相互转换问题,这里做了个整理.包含的内容如下: Bitmap和BitmapImage相互转换. RenderTargetBitmap ...
- Stream 和 byte[] 之间的转换
Stream 和 byte[] 之间的转换 一. 二进制转换成图片 ? 1 2 3 4 5 MemoryStream ms = new MemoryStream(bytes); ms.Position ...
随机推荐
- HTTP 错误 500.21 - Internal Server Error 处理程序“PageHandlerFactory-ISAPI-4.0_32bit”在其模块
问题: 系统是win7.今天把我做过的项目发布后,在IIS上运行时一直出现一个错误,HTTP 错误500.21-Internal Server Error.处理程序“PageHandlerFactor ...
- 微信授权登陆接入第三方App(步骤总结)Android
微信授权登陆接入第三方App(步骤总结)Android Android App实现第三方微信登录
- !!!!OpenWrt系列教程汇总
OpenWrt FAQ https://dev.openwrt.org.cn/wiki/faqs OpenWrt编译教程 完全新手教程:openwrt编译全过程(sse) 直接编译出带中文的openw ...
- HDU1394 Minimum Inversion Number(线段树OR归并排序)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- 虚函数virtual
简单地说,那些被virtual关键字修饰的成员函数,就是虚函数.虚函数的作用,用专业术语来解释就是实现多态性(Polymorphism),多态性是将接口与实现进行分离:用形象的语言来解释就是实现以共同 ...
- YYmodel 郭耀源 底层分析
http://www.tuicool.com/articles/meAzIny YYModel 简介与使用 http://www.jianshu.com/p/663c7b608ff5 ...
- java与.net比较学习系列(3) 基本数据类型和类型转换
在Java中,数据类型分为两类,一类是基本数据类型,另外一类是引用类型. 而在C#中,数据类型分为三类,分别是基元类型,值类型和引用类型.其中基元类型是.net framework框架中预定义的类型, ...
- 传输中文乱码js解决方法
encodeURI要编码两次 var a="我的"; //编译两次 //window.location.href = "http://127.0.0.1:8080/kab ...
- java获取当前系统毫秒,纳秒
//获取当前系统毫秒 System.out.println(System.currentTimeMillis()); //获取当前系统纳秒 System.out.println(System.nano ...
- ora-24247:网络访问被访问控制列表(ACL)拒绝
用dba账户使用下面脚本授予报错账户访问外部网络服务的权限,以SCOTT为例: BEGIN -- Only uncomment the following line if ACL "netw ...