簡體   English   中英

保存圖片框中的圖像

[英]Save the image from the picturebox

使用此代碼保存 PictureBox 中的圖像。 通過保存文件並為其添加名稱我面臨的問題,來吧,在調整圖像時發生以下錯誤: - gdi+ 中發生一般錯誤

我的代碼

show image

Using fs As New System.IO.FileStream("path", IO.FileMode.Open, IO.FileAccess.Read) PIC_PARTA.Image = System.Drawing.Image.FromStream(fs)  End Using 
save image
   Dim pic As Image
            pic = PIC_PARTA.Image
            Dim Savefild As New SaveFileDialog
            Savefild.FileName = TEXT_NAMEIMG.Text & ".PNG"
            Savefild.ShowDialog()
            pic.Save(Savefild.FileName, System.Drawing.Imaging.ImageFormat.Png)
            LAB_path.Text = Savefild.FileName
            Savefild.Dispose()
            pic.Dispose()

這不是現階段的完整答案,但它包含大量代碼,因此我將作為答案發布,然后根據需要更新或刪除。

我剛剛嘗試了這段代碼,所有四個保存都有效,盡管我認為有些可能不會:

Imports System.IO

Public Class Form1

    Private ReadOnly folderPath As String = Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyPictures, "Test")

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        PictureBox1.Image = Image.FromFile(Path.Combine(folderPath, "Picture1.png"))

        Using fs = File.OpenRead(Path.Combine(folderPath, "Picture2.png"))
            PictureBox2.Image = Image.FromStream(fs)
        End Using

        PictureBox3.ImageLocation = Path.Combine(folderPath, "Picture3.png")

        PictureBox4.Load(Path.Combine(folderPath, "Picture4.png"))
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            PictureBox1.Image.Save(Path.Combine(folderPath, "Output1.png"))
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Try
            PictureBox2.Image.Save(Path.Combine(folderPath, "Output2.png"))
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Try
            PictureBox3.Image.Save(Path.Combine(folderPath, "Output3.png"))
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Try
            PictureBox4.Image.Save(Path.Combine(folderPath, "Output4.png"))
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
    End Sub

End Class

您能否用您自己文件夾中的圖像進行測試,看看是否得到相同的結果?

請注意, Image.FromFile選項會鎖定文件,直到您處置Image 過去我曾看到嘗試保存使用Image.FromStream創建的Image會生成您提到的錯誤消息的實例,因為它稍后無法再次訪問 stream。 Image.FromFile避免了這種情況,但也有其自身的缺點。

我直接使用FileStream的代碼更簡單,因此比你的更好,但設置ImageLocation或調用Load再次更簡單,因此再次更好。 我建議您使用這兩個選項之一,看看您是否還有問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM