【问题标题】:Add click event to open individual picture box image添加点击事件打开单个图片框图片
【发布时间】:2016-08-02 19:19:57
【问题描述】:

我正在将图片框中的图像添加到流程布局面板。我正在尝试添加一个单击事件,以便在流程布局面板中单击图像时,它将打开原始图像。我的图片是.jpg。这是我到目前为止得到的,但似乎它不起作用。

For Each pic As FileInfo In New DirectoryInfo("picturepath").GetFiles("file.jpg")
    Dim picture As New PictureBox
    picture .Height = 113
    picture .Width = 145
    picture .BorderStyle = BorderStyle.Fixed3D
    picture .SizeMode = PictureBoxSizeMode.Zoom
    picture .Image = Image.FromFile(fi.FullName)

    AddHandler picture.MouseClick, AddressOf pictureBox_MouseClick
    flowlayoutpanel.Controls.Add(picture)
Next

Public Sub pictureBox_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
     ====>>> Not sure what goes here to get the correct path of that image since there could be more than one images.
End Sub

【问题讨论】:

  • 你能添加你的picturePreview_MouseClick函数吗
  • @jonju 小文本错误。
  • 首先您需要保存源文件的名称/路径 (fi.FullName) 以便以后访问它。文件路径未编码到图像中

标签: vb.net picturebox flowlayout


【解决方案1】:

您需要使用“sender”参数来获取对被点击的图片框的引用:

Public Sub pictureBox_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    Dim pb As PictureBox = DirectCast(sender, PictureBox)
    ' ... now do something with "pb" (and/or "pb.Image") ...
End Sub

仅引用 PictureBox(如我上面的示例),您只会引用图像本身。如果您想要文件的完整路径,那么您必须以某种方式将该信息与 PictureBox 一起存储;使用 Tag 属性是一种简单的方法:

Dim picture As New PictureBox
...
picture.Image = Image.FromFile(fi.FullName)
picture.Tag = fi.Fullname

现在您可以在点击事件中检索该文件名并对其进行处理:

Public Sub pictureBox_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    Dim pb As PictureBox = DirectCast(sender, PictureBox)

    ' ... now do something with "pb" (and/or "pb.Image") ...
    Dim fileName As String = pb.Tag.ToString()
    Process.Start(fileName)
End Sub

【讨论】:

  • 如果我使用 process.start,默认使用的 windows 照片是什么?
  • 你想用这个达到什么目的?
  • 点击打开图片即可。
  • 附带说明,当您像这样使用 Image.FromFile() 和“缩放”时,即使您正在查看一个小的“缩略图”,加载整个图像也会占用内存。如果您以这种方式加载一堆较大的图像,您的系统将受到严重的内存影响。您需要动态加载整个图像,实际上从中制作一个较小的缩略图(并显示它),然后 Dispose() 原始大图像实例。
  • @Idle_Mind 嗯。我想我需要将图像作为副本而不是原件打开,因为如果我以我拥有的方式打开它,它会将图片保存在我的应用程序中,因此如果我想从文档中打开原始图像,它会说我的应用程序打开了吗?无论如何围绕这个?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-13
  • 1970-01-01
  • 2016-09-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多