Groups | Blog | Home
all groups > dotnet drawing api > april 2007 >

dotnet drawing api : Unable to clone an image


Steven Garrad
4/27/2007 11:37:17 AM
Hi There, I need a little help.

I have a class uses the PictureBox as a base.

In this I want to store the current image twice. So I can restore the image
when needed.
Right now I create another Image object and when the user hits an apply
button it saves the current image into the second image. I use the
following:

undoImage = (System.Drawing.Image)this.Image.Clone();

That line appears to work, but my next section causes an
OutOfMemoryException Error:

using (Graphics g = Graphics.FromImage(this.Image))
{
g.FillRectangle(myBrush, myRectangle);
this.Invalidate();
g.Dispose();
}


What I don't understand is that if I comment out the line that makes a clone
of the image it works fine.

Does anyone know how to do this, or a different way or why this would cause
a out of memory exception?

Thanks,
Steve
Steven Garrad
4/27/2007 1:26:43 PM
Well I managed to answer it myself...

I save the image as a bitmap:

undoBitmap = new Bitmap(this.Image);

And then draw the bitmap back to the image using a graphic object:

g.DrawImage(System.Drawing.Image.FromHbitmap(undoBitmap.GetHbitmap()), 0, 0,
undoBitmap.Width, undoBitmap.Height);

The only strange thing about this is that I have to use the width and height
information as for some reason it applies it around 30% smaller without.



[quoted text, click to view]
Bob Powell [MVP]
4/28/2007 3:26:48 PM
The reason the size seems different is because the image resolution is
different to that of the screen and the system is compensating.


--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


[quoted text, click to view]
AddThis Social Bookmark Button