【发布时间】:2016-09-16 09:20:39
【问题描述】:
我从事一个小型实时项目,其中非常需要快速位图渲染技术。我需要每秒在图片框中显示许多(数百)个小块,我从pinvoke.net 网站找到了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等。