How do I draw a shape on an existing image?

Hello,

I've been trying to figure this out for a while now. I have an existing image that I've loaded into a variable via system.drawing.image.

How would I put a shape(circle, line, etc) in a location on that image

Thanks for any help.

P.S. I'm using .Net 2.0 and VB.NET 2005



Answer this question

How do I draw a shape on an existing image?

  • Rajaraman_85

    Ok, I found the following code that does what I need(with one exception)

    Dim g As Graphics = Me.CreateGraphics()

    g.Clear(Me.BackColor)

    ' Draw an image

    Dim curImage As Image = Image.FromFile("c:\untitled1.bmp")

    g.DrawImage(curImage, 0, 0, curImage.Width, curImage.Height)

    ' Create pens and a rectangle

    Dim rect As New Rectangle(220, 30, 100, 50)

    Dim opqPen As New Pen(Color.FromArgb(255, 0, 255, 0), 10)

    Dim transPen As New Pen(Color.FromArgb(128, 255, 255, 255), 10)

    Dim totTransPen As New Pen(Color.FromArgb(40, 0, 255, 0), 10)

    ' Draw lines, rectangle, ellipse and string

    g.DrawLine(opqPen, 10, 10, 200, 10)

    g.DrawLine(transPen, 10, 30, 200, 30)

    g.DrawLine(totTransPen, 10, 50, 200, 50)

    g.FillRectangle(New SolidBrush(Color.FromArgb(40, 0, 0, 255)), rect)

    rect.Y += 60

    g.FillEllipse(New SolidBrush(Color.FromArgb(50, 255, 255, 255)), rect)

    Dim semiTransBrush As New SolidBrush(Color.FromArgb(90, 255, 255, 50))

    g.DrawString("Some Photo " + ControlChars.Lf + "Date: 04/09/2001", New Font("Verdana", 14), semiTransBrush, New RectangleF(20, 100, 300, 100))

    ' Dispose

    g.Dispose()

    How would I save this new graphic to a new image file


  • j_ames2006

    Sweet!

    That is exactly what I was looking for.

    You are the man!

    It works perfectly for me now.

    Brent


  • Tjaalie

    What does Me.CreateGraphics() do If you're drawing to the screen, you should really use an OnPaint event handler.

    curImage.Save(filepath)

    Also, you don't seem to dispose of the image

    If you want to draw to the image without showing it on the screen, do this:

    Dim curImage As Image = Image.FromFile("c:\untitled1.bmp")

    Dim g as Graphics = Graphics.FromBitmap(curImage)

    This loads the image, and then creates a graphics object to use to draw on it.



  • How do I draw a shape on an existing image?