0

i know there are plenty of questions about how to draw on/in/above an ImageBox but i can't find a good solution to my problem. As you can see, i have some lines which basically work. The problem is that the drawed line isn't under my Cursor. For example: I press the MouseButton at the right edge on my picture, but the line is made somewhere in the middle. I tried nearly everything i can imagine but nothing helps. I hope someone can help me!

Just for you to know: It's a little app which makes a Screenshot of the Screen where the Cursor is, this Screenshot is showed in an ImageBox and now i want to draw on it, for example to make clear what is so important on the Screenshot.

Here's the Code:

Dim previous As System.Nullable(Of Point) = Nothing

   Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
      previous = e.Location
   End Sub

   Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
      If previous IsNot Nothing Then
         Using g As Graphics = Graphics.FromImage(PictureBox1.Image)
            g.DrawLine(Pens.Red, previous.Value, e.Location)
         End Using
         PictureBox1.Invalidate()
         previous = e.Location
      End If
   End Sub

   Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
      previous = Nothing
   End Sub
  • 2
    You should check [this](https://www.vbforums.com/showthread.php?426684) out. – jmcilhinney Sep 17 '20 at 11:34
  • 1
    [How to use the Paint event to draw shapes at mouse coordinates](https://stackoverflow.com/a/53708936/7444103) – Jimi Sep 17 '20 at 12:41
  • Hey, first of all: Thank you! Poorly, both solutions doesn't really work. With the Paint-Event I got the same problem: My drawn line isn't where the Cursor is. And to speak about the first Solution: When saving the picture, it saved only the picture without my drawn lines. I'm programming since 6 years now but never had such a problem where nothing seems to help. I doesn't even understand why it won't work – Lucas Schilz Sep 18 '20 at 10:39

0 Answers0