绘图事件绘制两个对象

时间:2016-01-04 12:47:01

标签: vb.net user-interface graphics drawing ram

程序只有这段代码。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    PictureBox1.Image = PictureBox2.Image
    Dim g As Graphics = Graphics.FromImage(PictureBox1.Image)
    g.DrawEllipse(New Pen(Color.MediumOrchid, 5), 30, 30, 30, 30)
    g.Dispose()
    PictureBox1.Refresh()
    PictureBox2.Refresh()
End Sub

点击PictureBox1之前是emtpty,PictureBox2有白色图片。 Before Clicking

点击后PictureBox1PictureBox2都有椭圆。enter image description here

我认为程序使用一个图像用于两个pictureBox'es.So当我绘画时它们都被绘制。我想用ellipse设置picbox2白色图像和picbox1白色图像。任何解决方案?

1 个答案:

答案 0 :(得分:-1)

您必须制作图像的副本,因此您将使用相同的数据,但不使用相同的对象,因此不会对原始对象进行更改。

我不是vb.net专家,但你可以试试这个:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
    Button1.Click
    PictureBox1.Image = New Bitmap(PictureBox2.Image)
    Dim g As Graphics = Graphics.FromImage(PictureBox1.Image)
    g.DrawEllipse(New Pen(Color.MediumOrchid, 5), 30, 30, 30, 30)
    g.Dispose()
    PictureBox1.Refresh()
    PictureBox2.Refresh()
End Sub