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
Catch ex As Exception
MessageBox.Show("Base64StringToImage 转换失败" & vbLf & "Exception:" & ex.Message)
End Try
End Sub