Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Drawing on a PictureBox

Drawing on a PictureBox

Scheduled Pinned Locked Moved Visual Basic
graphicshelpcsharpquestion
5 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RyanSmith
    wrote on last edited by
    #1

    Hi, I have switched to .NET only recently and I am having some trouble with the picturebox and drawing on it over an image. The aim is to have a picturebox with an image, then using a checkbox, be able to show and hide graphics over the image. In this case I am trying to draw a crosshair at the centre of the image. The examples I have come across on the web have been minimal help. I am also unclear about the paint event for the picturebox and the form. I suspect I should be calling the draw the crosshair sub in one or both of these events ... as well as in the checkbox event? I have created a simple, to show my problem. My hope is someone can set me straight here. Below is the code from my sample form.

    Public Class Form1

    Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'LOAD A IMAGE INTO THE PICTUREBOX
        PictureBox1.Load("SampleImage.jpg")
    End Sub
    
    Private Sub CheckBox1\_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then
            'Show the Cental Crosshair
            DisplayCentralXhair()
        Else
            'Do not show (erase?) the Central Crosshair
            RemoveCentralXhair()
        End If
    End Sub
    
    Private Sub DisplayCentralXhair()
        Dim mypen As Pen
        Dim aGraphics As Graphics
    
        mypen = New Pen(Color.Red, 1)
        aGraphics = PictureBox1.CreateGraphics()
    
        aGraphics.DrawLine(mypen, 182, 137 - 5, 182, 137 + 5)
        aGraphics.DrawLine(mypen, 182 - 5, 137, 182 + 5, 137)
    
    End Sub
    
    Private Sub RemoveCentralXhair()
        'Can I clear the Graphics drawn on the picturebox and retain the image below ?
        'Do I have to reload the image ?
    
    End Sub
    

    End Class

    L 1 Reply Last reply
    0
    • R RyanSmith

      Hi, I have switched to .NET only recently and I am having some trouble with the picturebox and drawing on it over an image. The aim is to have a picturebox with an image, then using a checkbox, be able to show and hide graphics over the image. In this case I am trying to draw a crosshair at the centre of the image. The examples I have come across on the web have been minimal help. I am also unclear about the paint event for the picturebox and the form. I suspect I should be calling the draw the crosshair sub in one or both of these events ... as well as in the checkbox event? I have created a simple, to show my problem. My hope is someone can set me straight here. Below is the code from my sample form.

      Public Class Form1

      Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          'LOAD A IMAGE INTO THE PICTUREBOX
          PictureBox1.Load("SampleImage.jpg")
      End Sub
      
      Private Sub CheckBox1\_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
          If CheckBox1.Checked = True Then
              'Show the Cental Crosshair
              DisplayCentralXhair()
          Else
              'Do not show (erase?) the Central Crosshair
              RemoveCentralXhair()
          End If
      End Sub
      
      Private Sub DisplayCentralXhair()
          Dim mypen As Pen
          Dim aGraphics As Graphics
      
          mypen = New Pen(Color.Red, 1)
          aGraphics = PictureBox1.CreateGraphics()
      
          aGraphics.DrawLine(mypen, 182, 137 - 5, 182, 137 + 5)
          aGraphics.DrawLine(mypen, 182 - 5, 137, 182 + 5, 137)
      
      End Sub
      
      Private Sub RemoveCentralXhair()
          'Can I clear the Graphics drawn on the picturebox and retain the image below ?
          'Do I have to reload the image ?
      
      End Sub
      

      End Class

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, this article[^] could point you in the right direction. BTW: the code shown is C#, the principles are valid in all .NET languages though. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      R 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, this article[^] could point you in the right direction. BTW: the code shown is C#, the principles are valid in all .NET languages though. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


        R Offline
        R Offline
        RyanSmith
        wrote on last edited by
        #3

        Thanks for the example Luc. Unfortunately, I am still not clear with my example if I have to redraw the image when I want to remove the graphics I have drawn ? Or can I clear the graphics without effecting the image below ? Cheers Ryan

        L 1 Reply Last reply
        0
        • R RyanSmith

          Thanks for the example Luc. Unfortunately, I am still not clear with my example if I have to redraw the image when I want to remove the graphics I have drawn ? Or can I clear the graphics without effecting the image below ? Cheers Ryan

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          A PictureBox has two layers: the image inside (which you can only access through the PictureBox.Image property), and whatever you draw on top of the PB (and best in the Paint handler). These two are not related at all (the image is below the glass plate, your scribbles are on top of the glass plate). I would use PB.Refresh() to erase the latter, if I ever were to use a PB that is. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


          R 1 Reply Last reply
          0
          • L Luc Pattyn

            A PictureBox has two layers: the image inside (which you can only access through the PictureBox.Image property), and whatever you draw on top of the PB (and best in the Paint handler). These two are not related at all (the image is below the glass plate, your scribbles are on top of the glass plate). I would use PB.Refresh() to erase the latter, if I ever were to use a PB that is. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


            R Offline
            R Offline
            RyanSmith
            wrote on last edited by
            #5

            Thanks Luc, The physical description "glass plate" you described has reinforced my own interpretation of how this PB works. The .Refresh() helped too. I suspect you would not choose a PB to do this task ? Thanks again. Ryan

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups