Image是顯示圖片的控制元件,若要顯示一張Bitmap的圖片,必須轉換成ImageSouce,并賦值給Souce,有如下幾種方式:
A:
private ImageSource ToBitmapSourceA(Bitmap bitmap)
{
MemoryStream stream = new MemoryStream();
bitmap.Save(stream, ImageFormat.Bmp);
stream.Position = 0;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = stream;
bitmapImage.EndInit();
return bitmapImage;
}
B:
[DllImport("gdi32")]
private static extern int DeleteObject(IntPtr o);
public ImageSource ToBitmapSourceB(Bitmap bitmap)
{
IntPtr ptr = bitmap.GetHbitmap(); //obtain the Hbitmap
BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(ptr, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
DeleteObject(ptr); //release the HBitmap
return bitmapSource;
}
C:
public ImageSource ToBitmapSourceC(Bitmap bitmap)
{
var bmpData = https://www.cnblogs.com/catzhou/archive/2021/02/01/bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
BitmapSource bitmapSource = BitmapSource.Create(bitmap.Width, bitmap.Height, 96, 96, PixelFormats.Bgr24, myPalette, bmpData.Scan0, bitmap.Width * bitmap.Height * 3, bitmap.Width * 3);
bitmap.UnlockBits(bmpData);
return bitmapSource;
}
在我的電腦上測驗:運行A需要5.2毫秒,B需2.6毫秒,C需1.2毫秒,顯然C是最快的,
更何況如果用opencv獲取攝像頭資料的話,還不需要轉換成Bitmap格式,直接用Mat.DataPointer就搞定了,
private ImageSource ToImageSourceD(Mat frame)
{
return BitmapSource.Create(frame.Width, frame.Height, 96, 96, PixelFormats.Bgr24, myPalette, frame.DataPointer, frame.Width * frame.Height * 3, frame.Width * 3);
}
是不是方便多了?
是不是方便多了?是不是方便多了?是不是方便多了?是不是方便多了?是不是方便多了?是不是方便多了?是不是方便多了?是不是方便多了?是不是方便多了?是不是方便多了?是不是方便多了?是不是方便多了?是不是方便多了?是不是方便多了?是不是方便多了?是不是方便多了?是不是方便多了?是不是方便多了?是不是方便多了?是不是方便多了?是不是方便多了?是不是方便多了?是不是方便多了?是不是方便多了?
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/255423.html
標籤:.NET技术
上一篇:從零搭建一個IdentityServer——初識OpenIDConnect
下一篇:怎么做一個專業的軟體安裝包?
