CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Hybrid View

  1. #1
    Join Date
    Apr 2011
    Posts
    2

    Out of Memory Exception (Graphics.DrawImage)

    Well...at first:
    I did use the search function and yes, I found tons of related posts and threads but in none of them I could find my answer.

    Now about the problem:
    I try to do some kind of 'pencil' that draws an 32 x 32 area of an image in an invisible 32x32spacing grid. And it works pretty well except the OoM Exception after some scribbling around.

    Codesnippet (Error is thrown in pb_map_pencil):
    Code:
    		private void pb_map_pencil(object sender, EventArgs e)
    		{
    			//Get MouseCoords
    			int x = (e as MouseEventArgs).X;
    			int y = (e as MouseEventArgs).Y;
    			x = (int)(x / 32.0);
    			y = (int)(y / 32.0);
    			//Check if targeted area already drawn
    			if (this.mouse_old_position == new Point(x, y))
    			{
    				return;
    			}
    			this.mouse_old_position = new Point(x, y);
    
    			//Drawing Code
    			Graphics g;
    			Image img = Image.FromFile(this.runtime.Source); //Load ImageSource
    			//if no Image exists on Picture Box, create one (looks like the source)
    			if (this.pb_map.Image == null)
    			{
    				this.pb_map.Image = img;
    			}
    			g = Graphics.FromImage(this.pb_map.Image); //create Graphics Object
    			//draw area
    			g.DrawImage(img, x * 32, y * 32, new Rectangle(this.runtime.Selector.X * 32, this.runtime.Selector.Y * 32, 32, 32), GraphicsUnit.Pixel); //(here's the Exception thrown)
    			//invalidate Picturebox
    			this.pb_map.Invalidate();
    		}
    
    //MouseControl on pictureBox
    		private void pb_map_MouseDown(object sender, MouseEventArgs e)
    		{
    			this.mouse_down = true;
    			this.mouse_old_position = new Point(-1,-1);
    			this.pb_map_pencil(sender, e);
    		}
    
    		private void pb_map_MouseMove(object sender, MouseEventArgs e)
    		{
    			if (this.mouse_down)
    			{
    				this.pb_map_pencil(sender, e);
    			}
    		}
    
    		private void pb_map_MouseUp(object sender, MouseEventArgs e)
    		{
    			this.mouse_down = false;
    		}
    Stacktrace:
    bei System.Drawing.Graphics.CheckErrorStatus(Int32 status)
    bei System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Rectangle srcRect, GraphicsUnit srcUnit)
    bei RGM.frm_main.pb_map_pencil(Object sender, EventArgs e) in C:\Users\xxx\documents\visual studio 2010\Projects\RGM\RGM\frm_main.cs:Zeile 136.
    bei RGM.frm_main.pb_map_MouseMove(Object sender, MouseEventArgs e) in C:\Users\xxx\documents\visual studio 2010\Projects\RGM\RGM\frm_main.cs:Zeile 152.
    bei System.Windows.Forms.Control.OnMouseMove(MouseEventArgs e)
    bei System.Windows.Forms.Control.WmMouseMove(Message& m)
    bei System.Windows.Forms.Control.WndProc(Message& m)
    bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    bei System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
    bei System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
    bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
    bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
    bei System.Windows.Forms.Application.Run(Form mainForm)
    bei RGM.Program.Main() in C:\Users\xxx\documents\visual studio 2010\Projects\RGM\RGM\Program.cs:Zeile 15.
    bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
    bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    bei System.Threading.ThreadHelper.ThreadStart()
    I think I overlooked something very basic but I don't get it
    Maybe it's about an using the Image or because nothing is explicit disposed by myself. But whenever I try to dispose the Image img by myself or using the using the main function throws an invalid argument exception at Application.Run without any usable infos for me...

    So any help with that problem would be great

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: Out of Memory Exception (Graphics.DrawImage)

    Dispose of your images and graphics when you're done with them. If that doesn't work, paste the error you get.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Apr 2011
    Posts
    2

    Re: Out of Memory Exception (Graphics.DrawImage)

    Wasn't that easy to dispose the Image, since this
    Code:
    this.pb_map.Image = img;
    img.dispose() does not only dispose the img alone but since its referenced also by the pictureBox.Image which still is needed (thats why the invalid paramter exceptions are thrown after dispose() i mentioned before).
    Took me a while until i saw that.

    Now I'm doing it this way:
    Code:
    //if no Image exists on Picture Box, create one (looks like the source)
    			if (this.pb_map.Image == null)
    			{
    				this.pb_map.Image = (Image)img.Clone();  //Make it not the Image itself but a clone of it (so im can be disposed)
    			}
    			g = Graphics.FromImage(this.pb_map.Image); //create Graphics Object
    			//draw area
    			g.DrawImage(img, x * 32, y * 32, new Rectangle(this.runtime.Selector.X * 32, this.runtime.Selector.Y * 32, 32, 32), GraphicsUnit.Pixel);
    			img.Dispose();
    Now I can't reproduce the OoM-Exception but to be honest, I can't believe that this solution is the best (I don't like to copy or to clone...) so I'm asking what do you think about this idea?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured