Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何保存图像文件?_.net_Vb.net - Fatal编程技术网

.net 如何保存图像文件?

.net 如何保存图像文件?,.net,vb.net,.net,Vb.net,我有这段代码: Dim myStream As Stream = Nothing Dim openFileDialog1 As New OpenFileDialog() openFileDialog1.InitialDirectory = "C:\Users\Desktop\Sample Pictures" openFileDialog1.Filter = "Pictures|*.jpg|Text|*.txt" If openFileDialog1.S

我有这段代码:

    Dim myStream As Stream = Nothing
    Dim openFileDialog1 As New OpenFileDialog()

    openFileDialog1.InitialDirectory = "C:\Users\Desktop\Sample Pictures"
    openFileDialog1.Filter = "Pictures|*.jpg|Text|*.txt"

    If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            myStream = openFileDialog1.OpenFile()
            If (myStream IsNot Nothing) Then

              ' Insert code to read the stream here.

            End If
        End Sub
    End Class
我需要将myStream中的文件复制到某个目标文件夹。 知道我如何实现它吗?

您可以使用:

Image img = Image.FromStream(myStream);

除此之外,还有:

img.Save("new location");

[样本在C#]

假设
myDestinationDir
是您要复制文件的路径

If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then 
    ' Extract the filename part from the full filename returned by openDialog.'
    Dim selectedFile As String = Path.GetFileName(openFileDialog1.FileName)
    ' Append to the destinationDir the filename extracted'
    Dim destFile As String = Path.Combine(myDestinationDir, selectedFile)
    ' Copy with overwrite (if overwrite is not desidered, use the overload with False as 3 arg'
    System.IO.File.Copy(openFileDialog1.FileName, destFile)
End Sub 

这会将所选文件复制到目标文件夹,覆盖同名的现有文件。

这是我为一些实验软件编写的真实代码片段

    Dim strPath As String
    Dim fStream As FileStream
    Dim fileName As String

    Try
   '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'Get path from textbox
    strPath = txtSavePath.Text.ToLower

    'Check directory existence, if not, create it
    If Directory.Exists(strPath) Then

    Else
        Try
            Directory.CreateDirectory(strPath)
        Catch ex As Exception

            MessageBox.Show("Error in Creating Directory, Code : " & ex.ToString)
        End Try

    End If

    'Set current active directory
    Directory.SetCurrentDirectory(strPath)

    'Create Filename
    fileName = txtFileName.Text.ToUpper & "_" & DateTime.Now.ToString("yyyyMMdd_HH_mm_ss")


    'Write/Create file
    fStream = File.Open(fileName & ".png", FileMode.Create)
    fStream.Write(byteData, 0, nLength)
    fStream.Close()
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'Display On Window
        picGraphDisplay.BackgroundImage = Image.FromFile(fileName & ".png")
        txtSavedFileName.Text = fileName & ".png"
    Catch ex As Exception
        CheckVisaStatus(errorStatus)
        MessageBox.Show(ex.ToString)

    End Try
在本例中,filestream将图像的字节数据写入文件。从何处获取字节图像取决于您。在这种情况下,一个好主意是为文件使用一个特定的名称,在这里使用系统的
DateTime.Now


希望它能给你一些想法。干杯

为什么要读取文件并重新创建?如果您只想复制到另一个目录,那么可以使用System.IO.File.copy()方法。或者使用My.Computer.FileSystem.CopyFile()。用flying file文件夹为您购买一个很好的动画,如果复制文件需要一段时间,则非常有用。并且可以取消。你似乎没有解决OP在这里的情况。
    Dim strPath As String
    Dim fStream As FileStream
    Dim fileName As String

    Try
   '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'Get path from textbox
    strPath = txtSavePath.Text.ToLower

    'Check directory existence, if not, create it
    If Directory.Exists(strPath) Then

    Else
        Try
            Directory.CreateDirectory(strPath)
        Catch ex As Exception

            MessageBox.Show("Error in Creating Directory, Code : " & ex.ToString)
        End Try

    End If

    'Set current active directory
    Directory.SetCurrentDirectory(strPath)

    'Create Filename
    fileName = txtFileName.Text.ToUpper & "_" & DateTime.Now.ToString("yyyyMMdd_HH_mm_ss")


    'Write/Create file
    fStream = File.Open(fileName & ".png", FileMode.Create)
    fStream.Write(byteData, 0, nLength)
    fStream.Close()
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'Display On Window
        picGraphDisplay.BackgroundImage = Image.FromFile(fileName & ".png")
        txtSavedFileName.Text = fileName & ".png"
    Catch ex As Exception
        CheckVisaStatus(errorStatus)
        MessageBox.Show(ex.ToString)

    End Try