Object in use exception when accessing a Graphics object

G

Guest

Hi,

I have create a Graphics object from a HWND
Both the Window and the Graphics object are created by the same thread,
nevertheless when i dispose the object, I get an exception indicating that
the object is in use some where else.

Any comments?

Best regards
Martin

P.S.: Code looks like:

myGraphics=Graphics.FromHwnd(Parent.Parent.Handle);
parentColorValue=GetPixel(myGraphics.GetHdc(),1,Top+(Height/2));
myGraphics.Dispose();

P.P.S: exception in detail:

A first chance exception of type 'System.InvalidOperationException' occurred
in system.drawing.dll

Additional information: The object is currently in use elsewhere.
 
M

michael horn

Yay! I figured out what causesd this.

If you GetPixel with x/y coords outside the bitmap (ie <0 or >Height) it
will hold the bitmap open and throw this exception.

I fixed the problem by putting the getpixel code and all associated code in
a big if..end if block:

if not(pt.x<0 or pt.x>imgOutput.Width or pt.y<0 or pt.y>imgOutput.Height)
then

dim originalColor as System.Drawing.Color=imgOutput.getPixel(pt.x,pt.y)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top