如何在vb.net中放大图片框中的图像

时间:2011-05-04 18:07:00

标签: c# vb.net winforms drag-and-drop

我想设计视觉匹配对。将有两列。左列将显示图像,右列将具有文字标签。用户必须将图像拖到正确的标签上。

由于表单尺寸较小,因此图像必须更小(缩略图)。因此,当用户将鼠标悬停在图像上时,也应该有图像放大。用户还应该能够对图像进行基本的拖放操作。

那么如何实现这两个目标呢?

  1. 将图片框拖放到标签

  2. Picturebox图片放大?

1 个答案:

答案 0 :(得分:2)

1)将PictureBox拖放到标签:

首先,您必须将标签的AllowDrop属性设置为True(也可以在设计器中完成):

Label.AllowDrop = True

处理PictureBox.MouseDown以激活DragDrop:

 Private Sub PictureBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
                               Handles PictureBox1.MouseDown

    PictureBox1.DoDragDrop(PictureBox1, DragDropEffects.All)

End Sub

现在,处理Label的DragEnter和DragDrop事件:

Private Sub Label1_DragDrop(ByVal sender As Object, _
           ByVal e As System.Windows.Forms.DragEventArgs) Handles Label1.DragDrop

    'Get the data being dropped from e.Data and do something.

End Sub

Private Sub Label1_DragEnter(ByVal sender As Object, _
          ByVal e As System.Windows.Forms.DragEventArgs) Handles Label1.DragEnter

    'e.Effect controls what type of DragDrop operations are allowed on the label.
    'You can also check the type of data that is being dropped on the label here
    'by checking e.Data.

    e.Effect = DragDropEffects.All
End Sub

2)放大MouseOver上的PictureBox:

创建一个只包含PictureBox的新表单。当我们想要显示放大的图像时,这是我们将要显示的形式,我们称之为Form2。现在只需处理缩略图框的MouseHover事件:

 Private Sub PictureBox1_MouseHover(ByVal sender As Object, _
                   ByVal e As System.EventArgs) Handles PictureBox1.MouseHover

    'PictureBox1 is the thumbnail on the original form.
    'PictureBox2 is the full size image on the popup form.

    Form2.PictureBox2.ClientSize = PictureBox1.Image.Size
    Form2.PictureBox2.Image = CType(PictureBox1.Image.Clone, Image)
    Form2.ShowDialog()

End Sub

您需要了解如何处理弹出窗体。