Quantcast
Viewing all articles
Browse latest Browse all 12583

Object is currently in use elswhere

The following method keeps on generating an exception about the object being currently in use elsewhere.

public void testMethod(int imageNum)
{
imageOne = new Bitmap(50, 50);
panelB.DrawToBitmap(imageOne, new Rectangle(0, 0, 50,50));
imageOne.Save("H:\\" + imageNum + ".bmp", ImageFormat.Bmp);
panelA.BackgroundImage = imageOne;
imageTwo = myMethod();
panelB.BackgroundImage = imageTwo;
}

Here is the exception and StackTrace.

A first chance exception of type 'System.AccessViolationException' occurred in System.Drawing.dll
A first chance exception of type 'System.InvalidOperationException' occurred in System.Drawing.dll
Message: Object is currently in use elsewhere.
TargetSite: Int32 get_Flags()
Source: System.Drawing
InnerException: 
StackTrace:    at System.Drawing.Image.get_Flags()
   at System.Windows.Forms.ControlPaint.IsImageTransparent(Image backgroundImage)
   at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle, Color backColor, Point scrollOffset)
   at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle)
   at System.Windows.Forms.Control.OnPaintBackground(PaintEventArgs pevent)
   at System.Windows.Forms.ScrollableControl.OnPaintBackground(PaintEventArgs e)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
   at System.Windows.Forms.Control.OnPrint(PaintEventArgs e)
   at System.Windows.Forms.Control.WmPrintClient(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
   at System.Windows.Forms.Control.DefWndProc(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.Control.DrawToBitmap(Bitmap bitmap, Rectangle targetBounds)
   at Testing.Test.testMethod(Int32 imageNum) in Test.cs:line 72

Not sure how to solve the problem. Please help!


Viewing all articles
Browse latest Browse all 12583

Trending Articles