我工作在一个小型的实时项目,其中一个快速的位图渲染技术是非常必要的。我需要每秒在一个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