VB.NET 2010将图片从OpenFile对话框传递到另一个表单

时间:2012-12-11 07:01:31

标签: vb.net

我正在创建一个贷款程序,打开一个自定义表单对话框,您选择图片,单击打开,然后需要将其传递到另一个表单,以便在从对话框中单击确定后使用。当我从自定义对话框表单中单击“徽标文件”按钮时,这是我的代码。

表单被称为对话框表单,我需要将图片发送到NewLoanCaculatorForm以填充表单中的图片区域。

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

Dim mystream As Stream = Nothing

    'Open the File to pickup icon for Loan Calculator
    Dim OpenFileDialog1 As New OpenFileDialog

    'Set up and display the open File Dialog
    With OpenFileDialog1
        'Begin in the current Directory
        .InitialDirectory = "C:\"
        .Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
    End With

    If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
        Try
            mystream = OpenFileDialog1.OpenFile()
            If (mystream IsNot Nothing) Then
                ' I believe the coded goes here but I'm stuck 
            End If
        Catch Ex As Exception
            MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
        Finally
            ' Check this again, since we need to make sure we didn't throw an exception on open. 
            If (mystream IsNot Nothing) Then
                mystream.Close()
            End If
        End Try
    End If

End Sub

1 个答案:

答案 0 :(得分:0)

这就是我通常做的事情:

在DialogForm中创建全局变量:

Public Property sPath as String 

Public Property imgLogo as Image

要获取图像,请执行以下操作:

imgLogo = Image.FromFile(OpenFileDialog1.FileName)

或者干脆做:

sPath = OpenFileDialog1.FileName

而不是

mystream = OpenFileDialog1.OpenFile()

然后,当您完成此操作后,单击“确定”按钮或任何您调用它的方式关闭表单。

然后在您的主要表单NewLoanCaculatorForm中调用DialogForm,您只需执行以下操作:

img = DialogForm.imgLogo

path = DialogForm.sPath
img = Image.FromFile(path)

取决于您在DialogForm中存储信息的方式。

此外,如果您正在寻找图像,我建议您不要在过滤器中使用.txt。这将严重破坏执行。