VB.NET 图片与BASE64互转

 Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        Dim dlg As OpenFileDialog = New OpenFileDialog()
        dlg.Multiselect = True
        dlg.Title = "选择要转换的图片"
        dlg.Filter = "Image files (*.jpg,*.bmp,*.gif,*.png)|*.jpg;*.jpeg;*.gif;*.bmp|AllFiles (*.*)|*.*"

        If DialogResult.OK = dlg.ShowDialog() Then

            For i As Integer = 0 To dlg.FileNames.Length - 1

                RichTextBox1.Text = ImgToBase64String(dlg.FileNames(i).ToString())

            Next



        End If



    End Sub
Function ImgToBase64String(ByVal Imagefilename As String)
        Try
            Dim bmp As Bitmap = New Bitmap(Imagefilename)
            Me.PictureBox1.Image = bmp
            Dim fs As FileStream = New FileStream(Imagefilename & "_BASE64" & ".txt", FileMode.Create)
            Dim sw As StreamWriter = New StreamWriter(fs)
            Dim ms As MemoryStream = New MemoryStream()
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
            Dim arr As Byte() = New Byte(ms.Length - 1) {}
            ms.Position = 0
            ms.Read(arr, 0, CInt(ms.Length))
            ms.Close()
            Dim strbaser64 As String = Convert.ToBase64String(arr)

            sw.Write(strbaser64)
            sw.Close()
            fs.Close()
            Return strbaser64

        Catch ex As Exception
            MessageBox.Show("ImgToBase64String 转换失败" & vbLf & "Exception:" & ex.Message)
        End Try
    End Function
 Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

        Dim dlg As OpenFileDialog = New OpenFileDialog()
        dlg.Multiselect = True
        dlg.Title = "选择要转换的base64编码的文本"
        dlg.Filter = "txt files|*.txt"

        If DialogResult.OK = dlg.ShowDialog() Then

            For i As Integer = 0 To dlg.FileNames.Length - 1

                Base64StringToImage(dlg.FileNames(i).ToString())
                RichTextBox1.Text = File.ReadAllText(dlg.FileNames(i))
            Next

        End If

    End Sub
Private Sub Base64StringToImage(ByVal txtFileName As String)
        Try
            Dim ifs As FileStream = New FileStream(txtFileName, FileMode.Open, FileAccess.Read)
            Dim sr As StreamReader = New StreamReader(ifs)
            Dim inputStr As String = sr.ReadToEnd()
            Dim arr As Byte() = Convert.FromBase64String(inputStr)
            Dim ms As MemoryStream = New MemoryStream(arr)
            Dim bmp As Bitmap = New Bitmap(ms)
            ms.Close()
            sr.Close()
            ifs.Close()
            Me.PictureBox1.Image = bmp

            'If File.Exists(txtFileName) Then
            ' File.Delete(txtFileName)
            'End If

        Catch ex As Exception
            MessageBox.Show("Base64StringToImage 转换失败" & vbLf & "Exception:" & ex.Message)
        End Try
    End Sub
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值