1: Imports System.IO
2: Imports System.Drawing.Imaging
3:
4: Public Class ImageHelper
5:
6: ' Convert an image to a Base64 string by using a MemoryStream. Save the
7: ' image to the MemoryStream and use Convert.ToBase64String to convert
8: ' the content of that MemoryStream to a Base64 string.
9: Public Shared Function ImageToBase64String(ByVal image As Image, _
10: ByVal imageFormat As ImageFormat)
11:
12: Using memStream As New MemoryStream
13:
14: image.Save(memStream, imageFormat)
15:
16: Dim result As String = Convert.ToBase64String(memStream.ToArray())
17:
18: memStream.Close()
19:
20: Return result
21:
22: End Using
23:
24: End Function
25:
26: ' Convert a Base64 string back to an image. Fill a MemorySTream based
27: ' on the Base64 string and call the Image.FromStream() methode to
28: ' convert the content of the MemoryStream to an image.
29: Public Shared Function ImageFromBase64String(ByVal base64 As String)
30:
31: Using memStream As New MemoryStream(Convert.FromBase64String(base64))
32:
33: Dim result As Image = Image.FromStream(memStream)
34:
35: memStream.Close()
36:
37: Return result
38:
39: End Using
40:
41: End Function
42:
43: End Class