C# bitblt bitmap render to control
<p>I work on a small real-time project where a fast bitmap rendering technique is  quite necessary. I need to display many(hundreds) small blocks in a picturebox per second,i found the <code>bitblt</code> example from the <a href="http://www.pinvoke.net/default.aspx/gdi32/BitBlt.html" rel="nofollow noreferrer">pinvoke.net</a> website.</p>
<p>I use a while loop(right now it's infinite ),to retrieve a particular bitmap,and then calling the <code>Invalidate()</code> method to trigger the <code>Paint</code> event.</p>
<p>This is my code:</p>
<pre><code>    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(() =&gt; this.Invalidate()));//trigger the repaint event
            }

        }
    }
</code></pre>
<p>It works fine for few seconds,then i'm getting an <strong><code>System.ArgumentException</code></strong> on this line:</p>
<pre><code>BitBlt(pTarget, 0, 0, bmp.Width, bmp.Height, pSource, 0, 0, TernaryRasterOperations.SRCCOPY);
</code></pre>
<p>Does anyone have an idea what is wrong here? i keep releasing the used resources(in the paint event)...why i'm getting this error?</p>
<p>Thanks in advance.</p>