GDI Save Problem

I've been reading books and surfing the web for an answer to what, I thought, was a standard graphics problem I'm having...hopefully you can help.

I need to obtain an image from a file (bmp), display it on a form (probably a picturebox), draw on it (g.drawline(), etc.), and then save the image as a different BMP.

Here's a simple version of the code:

Dim g as graphics = Me.CreateGraphics()
' get image
Dim myImage as Image=Image.FromFile("MyPic.bmp")
' draw it
g.DrawImage(myImage,0,0,myImage.Width,myImage.Height)
' draw anything here
g.DrawEllipse(Pens.Red, 50, 0, 50, 100)
' save it
myImage.Save("c:\MyPic1.bmp", ImageFormat.Bmp)
'clean up
g.Dispose

and what I end up with, on the new saved file, is the original image without the drawing (ellipse)!

What am I doing wrong



Answer this question

GDI Save Problem

  • RichardS71

    Huy,

    Thank you...it was the 'bit' of info I needed!

    My disconnect was 'who owned the drawing surface (graphics object)'!

    Again, thank you,

    Bob


  • Kenneth Alexander

    Hi Bob,

    You're obtaining the Graphics from your Form and draw the image + ellipse on that Graphics, so the original image was not modified.

    You should obtain the Graphics object from the image instead

    Dim myImage As Image = Image.FromFile("MyPic.bmp")
    Dim g As
    Graphics = Graphics.FromImage(myImage)
    g.DrawEllipse(Pens.Red, 50, 0, 50, 100)
    myImage.Save(
    "c:\MyPic1.bmp", Imaging.ImageFormat.Bmp)

    Best regards,



  • GDI Save Problem