Results 1 to 5 of 5

Thread: How to draw on an image? [2005]

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2005
    Posts
    18

    How to draw on an image? [2005]

    I'm trying to draw a dot onto an image.
    I can do it is asp.NET but nothing is happening when I do the following as part of a Windows Application. An better example would be great.

    Any suggestion please?

    Imports System.Drawing
    Imports System.Drawing.Imaging
    Imports System.Drawing.Drawing2D

    Public Class DrawImagewithDot

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim objBitmap As New Bitmap("C:\myIMG.bmp")
    Dim g As Graphics = Graphics.FromImage(objBitmap)

    Dim redBrush As New SolidBrush(Color.Red)
    Dim blueBrush As New SolidBrush(Color.Blue)

    g.FillEllipse(blueBrush, 100, 206, 10, 10)

    g.FillEllipse(redBrush, 200, 180, 10, 10)

    g.DrawImage(objBitmap, 10, 10)

    End Sub


    End Class

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to draw on an image? [2005]

    I'm no GDI+ guru but that code appears to be creating a Bitmap object from a file, then drawing on that Bitmap, then drawing that Bitmap on itself, then discarding the Bitmap. You aren't displaying the Bitmap anywhere in that code, although that may only be illustrative and not what you're actually doing. I'm confused by this part though:
    VB Code:
    1. g.DrawImage(objBitmap, 10, 10)
    g is a Graphics object that will draw on your Bitmap, and you're telling it to draw that same Bitmap, so basically you're drawing the Bitmap on itself. Is that what you intended to do, or am I interpreting that code incorrectly?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Member
    Join Date
    Jun 2006
    Posts
    38

    Re: How to draw on an image? [2005]

    It looks to me as if you are drawing a bitmap over the dot you are drawing. Draw the bitmap onto the canvas, and then draw the dot.

    VB Code:
    1. g.FillEllipse(blueBrush, 100, 206, 10, 10)
    2. g.FillEllipse(redBrush, 200, 180, 10, 10)
    3. g.DrawImage(objBitmap, 10, 10)

    Change to:

    VB Code:
    1. g.DrawImage(objBitmap, 10, 10)
    2. g.FillEllipse(blueBrush, 100, 206, 10, 10)
    3. g.FillEllipse(redBrush, 200, 180, 10, 10)

    I will take a look at my GDI app and see what is happening if that isn't the problem.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Nov 2005
    Posts
    18

    Re: How to draw on an image? [2005]

    I've solved it.

    Somewhere I read that only .bmp's would be correct but this doesn't seem to be the issue here.

    Anyway..... this works and I'm happy!


    Protected Overrides Sub OnPaint(ByVal pe As PaintEventArgs)

    Dim g As Graphics = Me.CreateGraphics()
    g.Clear(Me.BackColor)

    ' Draw an image
    Dim curImage As Image = Image.FromFile("c:/myIMG.jpg")
    g.DrawImage(curImage, 0, 0, curImage.Width, curImage.Height)


    Dim redBrush As New SolidBrush(Color.Red)
    Dim blueBrush As New SolidBrush(Color.Blue)

    g.FillEllipse(blueBrush, 100, 206, 10, 10)

    g.FillEllipse(redBrush, 200, 180, 10, 10)

    g.Dispose()


    End Sub 'OnPaint

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2005
    Posts
    18

    Re: How to draw on an image? [2005]

    Sorry jmcilhinney and Brant. I was so excited to answer my own question that I didn't see that you'd already responded. Thanks so much.
    You will have gathered I'm pretty ignorant about how everything hangs together wrt graphics.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width