vb.net 我将如何使用打开文件对话框来选择一个图像,然后将该图像放入另一个表单的图片框中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20448686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:03:31  来源:igfitidea点击:

How would I use a Open File Dialog to select a Image and then put that image in a picturebox on another form?

vb.net

提问by jruder

How would I use a Open File Dialog to select a Image and then put that image in a picturebox on another form?

我将如何使用打开文件对话框来选择一个图像,然后将该图像放入另一个表单的图片框中?

Private Sub btnLogo_Click(sender As Object, e As EventArgs) Handles btnLogo.Click

    OpenFileDialog1.Title = "Please Select a File"
    OpenFileDialog1.InitialDirectory = "C:"
    OpenFileDialog1.ShowDialog()
    photo = OpenFileDialog1.FileName.ToString  

'I'm guessing this is wrong but I'm lost to what to do here.

“我猜这是错误的,但我不知道在这里做什么。

Then once I have selected a image; what would be the appropriate code to put that image into a picture box on the other image?

然后一旦我选择了一个图像;将该图像放入另一张图像上的图片框中的适当代码是什么?

Once again; thanks for the help and any help is very much appreciated.

再来一次; 感谢您的帮助,非常感谢您的帮助。

回答by Jens

If I understood you correctly then it is pretty easy:

如果我理解正确,那么这很容易:

Sub OpenAnImageInPicturebox(ByRef pb As PictureBox)
    Dim ofd As New OpenFileDialog
    ofd.Filter = "Bitmap|*.bmp|JPEG|*.jpg" 'If you like file type filters you can add them here
    'any other modifications to the dialog
    If ofd.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub
    Try
        Dim bmp As New Bitmap(ofd.FileName)
        If Not IsNothing(pb.Image) Then pb.Image.Dispose() 'Optional if you want to destroy the previously loaded image
        pb.Image = bmp
    Catch
        MsgBox("Not a valid image file.")
    End Try
End Sub

回答by bto.rdz

try this:

尝试这个:

photo = image.Fromfile( OpenFileDialog1.FileName)

Hope it helps

希望能帮助到你

回答by Amarbir

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 处理Button1.Click

    OpenFileDialog1.Title = "Please select a file"
    OpenFileDialog1.InitialDirectory = "c:"
    OpenFileDialog1.ShowDialog()
    PictureBox1.ImageLocation = OpenFileDialog1.FileName.ToString
    PictureBox1.Visible = True


End Sub