我工作在一個小型的實時項目,其中一個快速的位圖渲染技術是非常必要的。我需要每秒在一個picturebox中顯示很多(數百個)小塊,我從pinvoke.net網站上找到bitblt
示例。C#bitblt位圖渲染控制
我使用while循環(現在它是無限的),檢索一個特定的位圖,然後調用Invalidate()
方法來觸發Paint
事件。
這是我的代碼:
protected override void OnPaint(PaintEventArgs e)
{
IntPtr pTarget = e.Graphics.GetHdc();
IntPtr pSource = CreateCompatibleDC(pTarget);
IntPtr pOrig = SelectObject(pSource, bmp.GetHbitmap());
BitBlt(pTarget, 0, 0, bmp.Width, bmp.Height, pSource, 0, 0, TernaryRasterOperations.SRCCOPY);
DeleteObject(pOrig);
DeleteDC(pSource);
e.Graphics.ReleaseHdc(pTarget);
}
private void Display()
{
while (true)
{
frame = desktopDuplicator.GetLatestFrame();
if (frame != null)
{
bmp = frame.DesktopImage;//retrieve image.
this.Invoke(new Action(() => this.Invalidate()));//trigger the repaint event
}
}
}
它工作正常的幾秒鐘,然後我在這條線得到一個System.ArgumentException
:
BitBlt(pTarget, 0, 0, bmp.Width, bmp.Height, pSource, 0, 0, TernaryRasterOperations.SRCCOPY);
有沒有人有一個想法是什麼這裏錯了嗎?我不斷釋放使用的資源(在繪畫事件中)......爲什麼我得到這個錯誤?
在此先感謝。
在例外情況下'ParamName'的值是什麼?這應該給你一個提示,你有什麼對象有問題:'pTarget','bmp','pSource'等 – Gabrielius