Drawing event draws two object

别说谁变了你拦得住时间么 提交于 2019-12-13 04:46:58

问题


Program has only this code.

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

Before clicking PictureBox1 is emtpty and PictureBox2has a white image.

After clicked PictureBox1 and PictureBox2 both have ellipse.

I think program uses one image for two pictureBox'es.So when I paint they are both painted.I want to set picbox2 white image and picbox1 white image with ellipse.Any solution ?


回答1:


You have to make a copy of the image, so you will use the same data, but not the same object, and so are safe from changes on the original object.

I am not a vb.net expert, but you may try this:

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


来源:https://stackoverflow.com/questions/34591537/drawing-event-draws-two-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!