C# bitmap转换bitmapsource
2017-09-28 14:49
1456 查看
方法1:
方法2:
方法1是网上的,方法2是个人总结的,亲测都可以使用。
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(image); IntPtr hBitmap = bmp.GetHbitmap(); System.Windows.Media.ImageSource WpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); box.Source = WpfBitmap;
方法2:
[System.Runtime.InteropServices.DllImport("gdi32")] static extern int DeleteObject(IntPtr o); //Bitmap转BitmapSource public BitmapSource BitmapToBitmapSource(System.Drawing.Bitmap bitMap) { //System.Drawing.Bitmap newBitMap = CaptureBitmapFormBitMap(bitMap, new System.Drawing.Rectangle(0, 0, bitMap.Width, bitMap.Height)); IntPtr ip = bitMap.GetHbitmap(); BitmapSource bitmapSource = null; bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( ip, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromWidthAndHeight(bitMap.Width, bitMap.Height)); DeleteObject(ip); return bitmapSource; }
方法1是网上的,方法2是个人总结的,亲测都可以使用。
相关文章推荐
- 【C#/WPF】Bitmap、BitmapImage、ImageSource 、byte[]转换问题
- 【C#/WPF】Bitmap、BitmapImage、ImageSource 、byte[]转换问题
- C#将Bitmap转换成byte[],可以传给C++处理,与Opencv通信。
- WPF Image控件中的ImageSource与Bitmap的互相转换
- c# Bitmap byte[] Stream 文件相互转换
- c# Bitmap byte[] Stream 文件相互转换
- C#将Bitmap转换为Icon
- [C#] Byte[]、Image、Bitmap 之间的相互转换
- c# Byte[]、Image、Bitmap 之间的相互转换
- C# : 操作Word文件的API - (将C# source中的xml注释转换成word文档)
- C# 图像处理:Bitmap 与 Image 之间的转换
- WPF(C#)中Bitmap与BitmapImage相互转换
- WPF(C#)中Bitmap与BitmapImage相互转换
- c# bitmap和base64编码相互转换
- C# bitmap转换为字节流
- C#之BitmapData应用2 由数据转换为Bitmap图像
- c# Bitmap byte[] Stream 文件相互转换
- WPF Image控件中的ImageSource与Bitmap的互相转换
- WPF Image控件中的ImageSource与Bitmap的互相转换
- c# Bitmap byte[] Stream 文件相互转换