【问题标题】:Obtain properties of a selected image获取所选图像的属性
【发布时间】:2013-03-01 14:34:57
【问题描述】:

我在 vb.net 中写作,我正在使用 Visual Studio 2010 Professional

好的,我将展示我的代码,然后解释我需要帮助的地方。

Imports System.IO.File
Imports System.IO.Directory
Imports System.IO

Public Class ImageSelection

    Private Sub ImageSelectionbtn_Click(sender As System.Object, e As System.EventArgs) Handles ImageSelectionbtn.Click

        With OpenFileDialog1
            .Filter = _
"Image File (*.jpg)|*.jpg|Image File (*.jpeg)|*.jpeg|Image File (*.bmp)|*.bmp|Image File (*.gif)|*.gif"
            .InitialDirectory = System.Environment.SpecialFolder.MyPictures
            .Title = "Select a picture to open"

            If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

                ''''Here is where i need help''''
                Dim MyImage = OpenFileDialog1.FileName
                Dim MyImageWidth
                Dim MyImageHeight
                '''''''''''''''''''''''''''''''''

                With MyPicture 'MyPicture form

                    .Width = MyImageWidth
                    .Height = MyImageHeight
                    .PictureBox1.Image = Nothing
                    MyPicture.Show()
                End With
            End If
        End With
    End Sub
End Class

好的,所以我正在做的是当用户单击按钮时,它会打开一个打开的文件对话框,该对话框被过滤为仅允许 .jpg .jpeg .bmp 和 .gif

现在,当他们选择图片时,我需要一种能够从所选图片中提取少量信息的方法。

我需要图像高度和图像宽度,以便我可以将另一个表单(带有表单停靠的图像框)设置为该图像的大小。

我还需要帮助将第二个表单中的 picturebox 设置为他们选择的图像。

任何帮助将不胜感激。

【问题讨论】:

    标签: vb.net winforms openfiledialog


    【解决方案1】:
     With OpenFileDialog1
         .Filter = _
         "Image File (*.jpg)|*.jpg|Image File (*.jpeg)|*.jpeg|Image File (*.bmp)|*.bmp|Image File (*.gif)|*.gif"
         .InitialDirectory = System.Environment.SpecialFolder.MyPictures
         .Title = "Select a picture to open"
    
         If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
             ''''Here is where i need help''''
             Dim MyImage = OpenFileDialog1.FileName
             Dim image As Image = System.Drawing.Bitmap.FromFile(MyImage) 'Convert to Image from the selected file
    
             Dim MyImageWidth As Integer = image.Width 'Get The Width
             Dim MyImageHeight As Integer = image.Height 'Get The Height
    
             '''''''''''''''''''''''''''''''''
             With MyPicture 'MyPicture form
    
                 .Width = MyImageWidth
                 .Height = MyImageHeight
                 .PictureBox1.Image = image
                 MyPicture.Show()
             End With
         End If
     End With
    

    【讨论】:

    • 会的!我还添加了一些其他内容,以使其更易于阅读并获得正确的文件夹路径。而不是使用.InitialDirectory = System.Environment.SpecialFolder.MyPictures ,我现在使用.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures):D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 2010-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    相关资源
    最近更新 更多