您的位置:首页 > 编程语言 > C#

C# bitmap转换bitmapsource

2017-09-28 14:49 1456 查看
方法1:

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# bitmap bitmapsour