Object in use exception when accessing a Graphics object

  • Thread starter Thread starter Guest
  • Start date Start date
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.
 
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)
 
Back
Top