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

时间:2013-12-08 01:09:43

标签: vb.net

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

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  

'我猜这是错的,但我不知道该怎么做。

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

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

4 个答案:

答案 0 :(得分:3)

如果我理解正确,那就很容易了:

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

答案 1 :(得分:2)

试试这个:

photo = image.Fromfile( OpenFileDialog1.FileName)

希望有所帮助

答案 2 :(得分:0)

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

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


End Sub

答案 3 :(得分:0)

我在寻找#!/bin/bash # Get server list set -f string=$DEPLOY_SERVER array=(${string//,/}) # Iterate servers for deploy and pull last commit for i in "${!array[@]}"; do echo "Deploy project on server ${array[i]}" ssh ec2-user@${array[i]} "cd backend && git pull && sudo cp -r /home/ec2-user/backend /var/ && cd /var/ && ./build_project.sh && cd /var/venv/bin/ && source ./activate && cd /var/backend && gunicorn myproject.wsgi:application --bind 0:8000 --daemon && sudo service nginx reload" done 时遇到了这个问题,作为对先前答案的更新。

您想使用Filter选择一个OpenFileDialog放在另一个Image中的PictureBox Control中。我建议:-

  1. 使用模块:在任何Form上使用图像
  2. 使用“默认图像”:在发生错误时在Form中显示。使用项目资源添加现有资源( IE PNG图像文件:noPhotoUsr)。

PictureBox Control的代码

Module1

要在任何Module Module1 Public Function _GetImgOFD(Frm As Form, PicBx As PictureBox) As Bitmap Dim _ErrBitmap As Bitmap = My.Resources.noPhotoUsr Dim ChosenBitmap As Bitmap Using OFD As OpenFileDialog = New OpenFileDialog With OFD .Filter = ("Image File (*.ico;*.jpg;*.bmp;*.gif;*.png)|*.jpg;*.bmp;*.gif;*.png;*.ico") .RestoreDirectory = True .Multiselect = False .CheckFileExists = True If .ShowDialog(Frm) = DialogResult.OK Then ChosenBitmap = Bitmap.FromFile(.FileName) Else ChosenBitmap = _ErrBitmap End If End With End Using Return ChosenBitmap End Function End Module PictureBox单击事件

中使用的代码
Form