VB.NET在PictureBox上绘制多条线而不删除以前的更改 [英] VB.NET drawing multiple lines on a PictureBox without removing previous changes

查看:103
本文介绍了VB.NET在PictureBox上绘制多条线而不删除以前的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在VB.NET PictureBox 上绘制多条线,而不删除以前的更改.如果我尝试在不同时间使用 e.graphics PictureBox 上绘制两条线,则它将删除先前的线/更改.

I'm trying to draw multiple lines on a VB.NET PictureBox without deleting the previous changes. If I try to draw two lines on a PictureBox during different times with e.graphics, then it would delete previous lines/changes.

我正在 PictureBox paint 事件上使用此代码.

I'm using this code on a PictureBox's paint event.

Public Class DrawLine

Dim point1X As Integer = 10
Dim point1Y As Integer = 10

Dim point2X As Integer = 50
Dim point2Y As Integer = 50

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    Dim pt As New Point(point1X, point1Y)
    Dim pt1 As New Point(point2X, point2Y)
    e.Graphics.DrawLine(Pens.Green, pt, pt1)    
End Sub

End Class

此示例完美地在 PictureBox 内画了一条绿线.

This example perfectly draws a green line inside the PictureBox.

但是,当窗体绘制完成后,当我有一个按钮在 PictureBox 上绘制另一条线时,上一条绘制的线就会消失.

However, when I have a button that draws another line on the PictureBox after the form has already been painted, the previous line drawn disappears.

例如,如果我有这样的函数绘制另一条线

For example, if I have a function like this that draws another line

Private Function DrawAnotherLine(xPos1 As Integer, yPos1 As Integer, xPos2 As Integer, yPos2 As Integer)

point1X = xPos1
point1Y = yPos1

point2X = xPos2
point2Y = yPos2

PictureBox1.Invalidate()
End Function

它将删除绘制的第一行,而仅绘制第二行.我希望它在第一行的顶部绘制第二行,而不只是删除第一行,我该怎么做?

It deletes the first line drawn and only draws the second line. I want it draw the second line on top of the first line and not just delete the first line though, how would I do this?

(注意:我不能在函数中包括绘制第一行,因为该函数将在已绘制的行上方多次使用).

(Note: I cannot include drawing the first line in the function, as this function will be used multiple times on top of the lines its already drawn).

推荐答案

GDI +绘图非常简单:

GDI+ drawing is pretty simple:

  1. 声明一个或多个字段以存储代表图形的数据.
  2. 处理要绘制的控件的 Paint 事件.
  3. 在步骤2的事件处理程序中,从步骤1中声明的字段中获取数据并执行绘图.
  4. 要更改图形,请修改在步骤1中声明的字段,然后在控件上调用 Invalidate .

如果您希望能够绘制多条线,则首先需要一个表示单条线的数据结构,然后再在某个位置存储该数据结构的多个实例.最明显的方法是定义一个包含单行数据的类或结构,例如

If you want to be able to draw multiple lines then you first need a data structure that represents a single line and then somewhere to store multiple instances of that data structure. The most obvious way to do that is to define a class or structure that contains the data for a single line, e.g.

Public Class Line

    Public ReadOnly Property StartPoint As Point

    Public ReadOnly Property EndPoint As Point

    Public Sub New(startPoint As Point, endPoint As Point)
        Me.StartPoint = startPoint
        Me.EndPoint = endPoint
    End Sub

End Class

,然后声明一个引用该类型集合的字段:

and to then declare a field that refers to a collection of that type:

Private lines As New List(Of Line)

您的 Paint 事件处理程序然后绘制集合中的所有行:

Your Paint event handler then draws all the lines in the collection:

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    For Each line In lines
        e.Graphics.DrawLine(Pens.Black, line.StartPoint, line.EndPoint)
    Next
End Sub

要绘制一条新线,请向集合中添加一个新的 Line 对象,并 Invalidate 该控件:

To draw a new line, you add a new Line object to the collection and Invalidate the control:

Private Sub AddNewLine(startPoint As Point, endPoint As Point)
    lines.Add(New Line(startPoint, endPoint))
    PictureBox1.Invalidate()
End Sub

如何获得这些积分取决于您.您可以在 MouseDown 事件上记录该位置,然后在 MouseUp 事件上调用 AddNewLine ,或者可以完全执行其他操作.

How you get those points is up to you. You might record the location on a MouseDown event and then call AddNewLine on a MouseUp event or you might do something else entirely.

在调用 Invalidate 时,最好指定可能的最小区域,但这超出了此问题的范围.

It is also preferable to specify the smallest area possible when calling Invalidate, but that's beyond the scope of this question.

这篇关于VB.NET在PictureBox上绘制多条线而不删除以前的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆