EN
|
NL
dec 19

Geplaatst door: André Obelink (Host)
19-12-2008 22:30 

In some cases you want to convert an image to a string which can be stored in for example a XML file. Once the string is stored as simple text, you want also be able to convert these readable characters back to the original image. Fortunately this is very easy to achieve with classes from the .NET platform. The trick is to use a MemoryStream and save an image to it. Then convert the result of MemoryStream.ToArray() to a Base64 string. Retrieving the image back from the Base64 string is even more easy. Create a MemoryStream based on that string and use the Image.FromStream() method to get the original image. In this example I create a class, named ImageHelper. In this class I define the two methods: ImageToBase64String() and ImageFrom64BaseString(). By using the Base64 format you are sure that the result string will not contain any characters which will be corrupt your XML file, like a '<' or '>'.

   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

As you can see I have defined both methods as so called Shared methods. When using these Shared methods you are not forced to instantiate the class. You can simply use the method by calling the ClassName.MethodName(). In the code example below you can see how easy it is to convert an image to a Base64 string and vice versa. 

   1: Imports System.Drawing.Imaging
   2: 
   3: 
   4: Private Sub ImageToBase64StringButton_Click() Handles ImageToBase64Button.Click
   5: 
   6:     ' Convert the original image to a Base64 string
   7:     Base64TextBox.Text = ImageHelper.ImageToBase64String(SourcePictureBox.Image, _
   8:                                                          ImageFormat.Jpeg)
   9: 
  10: End Sub
  11: 
  12: Private Sub ImageFromBase64StringButton_Click() Handles ImageFromBase64Button.Click
  13: 
  14:     ' Convert a Base64 string to the original image
  15:     TargetPictureBox.Image = ImageHelper.ImageFromBase64String(Base64TextBox.Text)
  16: 
  17: End Sub

 To give you some idea how this code works... see the screen shot below..

ImageFromToBase64String

Happy coding!

Tags:

Uw naam:
Uw email:
(Optioneel) Email wordt alleen gebruikt om een Gravatar te tonen.
Uw website:
Onderwerp:
Commentaar:
Security Code
Neem de code over
Voeg commentaar toe   Annuleren 
Blog
Blog archive
 

Willem Dreeslaan 63, 7103 JE  Winterswijk |  Postbus 428, 7100 AK Winterswijk  |  M 06-47914464  |  E info@maryor.nl