Base 64 Format Conversion

Recently, I was working with an InfoPath solutions prototype. The idea behind the solution was that a user would be taking a digital photo (JPEG). As they downloaded the pictures to their local machines, they would be able to include these camera shots into an InfoPath form that contained additional descriptive fields. By default, InfoPath takes these types of binaries and converts them into a Base 64 format. Once converted this data is then saved to an XML file. One requirement of the solution was to convert this data back to its original JPEG format once it was saved to the server, so that it could be displayed or saved.

For an example application, I created a simple data source based on a picture box. The data source contained the following items.

Using the following code that utilizes the System.Drawing.Bitmap namespace, I was able to take the Base 64 converted XML and turn it back into a JPEG.

Private Sub BtnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnConvert.Click

Dim retnbit As Bitmap

Dim rdr As New XmlTextReader("saved.xml")

rdr.WhitespaceHandling = WhitespaceHandling.None

' Read the file. Stop at the Base64 element.

While rdr.Read()

If "my:field1" = rdr.Name Then

Dim base64txt = rdr.ReadElementString()

retnbit = BitmapFromBase64(base64txt)

Exit While

End If

End While

PicImage.Image = retnbit

End Sub

Public Function BitmapFromBase64(ByVal base64 As String) As System.Drawing.Bitmap

Dim oBitmap As System.Drawing.Bitmap

Dim memory As New System.IO.MemoryStream(Convert.FromBase64String(base64))

oBitmap = New System.Drawing.Bitmap(memory)


memory = Nothing

Return oBitmap

End Function

For those that may want to take a look at the solution, I have posted it for download.


Comments (0)

Skip to main content