Cast Graphics to Image in C#

Posted by WebDevHobo on Stack Overflow See other posts from Stack Overflow or by WebDevHobo
Published on 2010-04-12T15:27:31Z Indexed on 2010/04/12 15:32 UTC
Read the original article Hit count: 718

Filed under:
|
|

I have a pictureBox on a Windows Form.

I do the following to load a PNG file into it.

Bitmap bm = (Bitmap)Image.FromFile("Image.PNG", true);
Bitmap tmp;

public Form1() {
    InitializeComponent();
    this.tmp = new Bitmap(bm.Width, bm.Height);
}

private void pictureBox1_Paint(object sender, PaintEventArgs e) {
    e.Graphics.DrawImage(this.bm, new Rectangle(0, 0, tmp.Width, tmp.Height), 0, 0, tmp.Width, tmp.Height, GraphicsUnit.Pixel);
}

However, I need to draw things on the image and then have the result displayed again. Drawing rectangles can only be done via the Graphics class.

I'd need to draw the needed rectangles on the image, make it an instance of the Image class again and save that to this.bm

I can add a button that executes this.pictureBox1.Refresh();, forcing the pictureBox to be painted again, but I can't cast Graphics to Image. Because of that, I can't save the edits to the this.bm bitmap.

That's my problem, and I see no way out.

© Stack Overflow or respective owner

Related posts about c#

Related posts about graphics