我從USB獲取圖像的序列,並抓住每個圖像我將抓取的結果轉換爲System.Drawing.Bitmap,然後將其轉換爲System.Windows.Mesia.Imging.BitmapImage以便能夠將它分配給Imagesource和最後在調度程序線程中更新UI,所有這些過程都需要時間並且不會實時生效,相機公司(Basler)的示例代碼使用C#並直接將System.Drawing.Bitmap分配給圖片框,並且可以不顯示實時視圖延遲。 什麼是處理它的最佳解決方案?值得一提的是2048 * 2000像素大小的幀速率幾乎是50 fps的如何順序更改WPF中沒有UI延遲的圖像源?
PixelDataConverter converter = new PixelDataConverter();
Bitmap bitmap = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppRgb);
BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
converter.OutputPixelFormat = PixelType.BGRA8packed;
IntPtr ptrBmp = bmpData.Scan0;
converter.Convert(ptrBmp, bmpData.Stride * bitmap.Height, grabResult);
bitmap.UnlockBits(bmpData);
BitmapImage bitmapimage = new BitmapImage();
using (MemoryStream memory = new MemoryStream())
{
bitmap.Save(memory, ImageFormat.Bmp);
memory.Position = 0;
bitmapimage.BeginInit();
bitmapimage.StreamSource = memory;
bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
bitmapimage.EndInit();
bitmapimage.Freeze();
}
Dispatcher.Invoke(new Action(() =>
{
imgMain.Source = bitmapimage;
}));
這是公司爲C#示例代碼:
Bitmap bitmap = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppRgb);
// Lock the bits of the bitmap.
BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
// Place the pointer to the buffer of the bitmap.
converter.OutputPixelFormat = PixelType.BGRA8packed;
IntPtr ptrBmp = bmpData.Scan0;
converter.Convert(ptrBmp, bmpData.Stride * bitmap.Height, grabResult); //Exception handling TODO
bitmap.UnlockBits(bmpData);
// Assign a temporary variable to dispose the bitmap after assigning the new bitmap to the display control.
Bitmap bitmapOld = pictureBox.Image as Bitmap;
// Provide the display control with the new bitmap. This action automatically updates the display.
pictureBox.Image = bitmap;
if (bitmapOld != null)
{
// Dispose the bitmap.
bitmapOld.Dispose();
}
}
enter code here
嘗試設置OutputPixelFormat屬性到由所述WPF的BitmapSource類支持的格式(見[的PixelFormats](https://msdn.microsoft.com/en-us/library/system.windows.media .pixelformats(v = vs.110)的.aspx))。然後直接調用[BitmapSource.Create()](https://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapsource.create(v = vs.110).aspx)。 – Clemens