Re: Unable to clone an image

Tech-Archive recommends: Speed Up your PC by fixing your registry



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.



"Steven Garrad" <stevierg@xxxxxxxxxxxxxxxxxxx> wrote in message news:6987878C-9291-4366-9B9E-440EA41DADBA@xxxxxxxxxxxxxxxx
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

.


Quantcast