0
我正在嘗試使用writeablebitmap,它不斷更新並將其呈現爲圖像,但只顯示圖像的頂部,中間和底部的2/3是黑色的。我認爲這可能與PixelFormat有關,因爲writeablebitmap是bgr24,圖像是rgb24。這是我目前正在做的。WPF圖像只顯示頂部1/3的位置
int bufferSize = videoRenderer.VideoWidth * videoRenderer.VideoHeight;
byte[] frameBuffer = new byte[bufferSize];
Marshal.Copy(videoRenderer.Bitmap.BackBuffer, frameBuffer, 0, bufferSize);
using (Bitmap frame = new Bitmap(videoRenderer.VideoWidth, videoRenderer.VideoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb))
{
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, videoRenderer.VideoWidth, videoRenderer.VideoHeight);
BitmapData bmpData = frame.LockBits(rect, ImageLockMode.ReadWrite, frame.PixelFormat);
Marshal.Copy(frameBuffer, 0, bmpData.Scan0, bufferSize);
frame.UnlockBits(bmpData);
IntPtr hBitmap = frame.GetHbitmap();
source = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
DeleteObject(hBitmap);
}
看起來有些可怕的錯誤,或者它最有可能是pixelformat。如果像素格式,如何通過c#中的每個像素交換藍色和紅色的?
我檢查writeablebitmap的backbufferstride,它返回1920.位圖的位圖數據返回它的步幅爲1920. – bl4kh4k
如果步幅匹配,我會檢查問題不會發生得更早。可寫位圖如何更新?也許在提供視頻的源代碼和可寫位圖之間有一個步幅差別。 –
videoRenderer是一個自定義對象,它保存writeablebitmap,它是1920. – bl4kh4k