PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Framework Forms The object is currently in use elswehere

Reply

The object is currently in use elswehere

 
Thread Tools Rate Thread
Old 13-12-2005, 06:17 PM   #1
Jeremy Chapman
Guest
 
Posts: n/a
Default The object is currently in use elswehere


Below is a snippit of code that is supposed to capture the image of a
visible window, and copy it to a picturebox control on my form. My
picturebox control is on a tab in a page control. Everything seems to work
fine except when I select the page that the picturebox is on to make it
visible, I get a System.InvalidOperationException in system.drawing.dll 'The
object is currently in use elsewhere'. Any ideas?

this.PictureBox1.Image = CaptureWindow(hwndWindowToCapture)

public static System.Drawing.Bitmap CaptureWindow(IntPtr
hWndWindowToCapture)
{
RECT pRect;
pRect.iLeft = 0;
pRect.iRight = 0;
pRect.iBottom = 0;
pRect.iTop = 0;

int iRetVal = User32.GetWindowRect(hWndWindowToCapture, ref pRect);

int iWidth = pRect.iRight - pRect.iLeft;
int iHeight = pRect.iBottom - pRect.iTop;

POINTAPI pPoint;
pPoint.iY = pRect.iTop;
pPoint.iX = pRect.iLeft;

iRetVal = User32.ScreenToClient(hWndWindowToCapture, ref pPoint);

pRect.iLeft = pPoint.iX;
pRect.iTop = pPoint.iY;

System.Drawing.Bitmap pBitmap = new System.Drawing.Bitmap(iWidth,
iHeight);

System.Drawing.Graphics pGraphics =
System.Drawing.Graphics.FromImage(pBitmap);

IntPtr iHDCWindow = User32.GetDC(hWndWindowToCapture);
try
{

IntPtr iHDCGraphic = pGraphics.GetHdc();

//System.Drawing.Graphics tg = System.Drawing.Graphics.FromHdc( hDc );
iRetVal = StretchBlt(
iHDCGraphic,
0,
0,
iWidth,
iHeight,
(int)iHDCWindow,
pRect.iLeft,
pRect.iTop,
iWidth,
iHeight,
SRCCOPY | CAPTUREBLT);
}
finally
{
User32.ReleaseDC(hWndWindowToCapture,iHDCWindow);
}
return pBitmap;
}


  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off