Image to Base64 works but with some strange things.

  • Thread starter Thread starter Kevin Kastelein
  • Start date Start date
K

Kevin Kastelein

Guest
Hi guys,

I've been working on converting Images to Base64.

I got this working but, as in the code below shows there is a MsgBox that tells me the pbPreview.ImageLocation.
If i remove this MsgBox the code won't work and tells me that its not assigned to an object.

Private Sub GegevensToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles GegevensToolStripMenuItem.Click
CollectAndConvertPictures()
End Sub

Private Sub CollectAndConvertPictures()
For Each Pictures As String In System.IO.Directory.GetDirectories(Dashboard.Werkmap.Text + "\Pictures")

PathField.Text = Dashboard.Werkmap.Text + "\Pictures\" + Path.GetFileName(Pictures) + "\" + Path.GetFileName(Pictures) + ".jpg"

pbPreview.ImageLocation = PathField.Text

MsgBox(pbPreview.ImageLocation) 'THIS IS THE MSGBOX THAT IS NOT NEEDED BUT WHEN REMOVED CODE WONT WORK
ConvertBase64()
Next
End Sub

Private Sub ConvertBase64()
Dim EncodingTypeString As String = String.Empty
If pbPreview.ImageLocation.ToLower.EndsWith(".jpg") Then
EncodeType = ImageFormat.Jpeg
EncodingTypeString = "data:image/jpeg;base64"
ElseIf pbPreview.ImageLocation.ToLower.EndsWith(".png") Then
EncodeType = ImageFormat.Png
EncodingTypeString = "data:image/png;base64"
ElseIf pbPreview.ImageLocation.ToLower.EndsWith(".gif") Then
EncodeType = ImageFormat.Gif
EncodingTypeString = "data:image/gif;base64"
ElseIf pbPreview.ImageLocation.ToLower.EndsWith(".bmp") Then
EncodeType = ImageFormat.Bmp
EncodingTypeString = "data:image/bmp;base64"
End If

DataFile.Text = DataFile.Text & EncodingTypeString & ImageToBase64(pbPreview.Image, EncodeType)
pbPreview.Image = Nothing
End Sub

Public Function ImageToBase64(ByVal image As Image, ByVal format As ImageFormat) As String
Dim ms As New MemoryStream
image.Save(ms, format)
Dim ImageBytes As Byte() = ms.ToArray
Dim Base64string As String = Convert.ToBase64String(ImageBytes)
Return Base64string

End Function



Someone can tell me why this MsgBox is needed?


Greetings :)



EDIT:

Okay So if i replace Msgbox(pbPreview.ImageLocation) with pbPreview.Load() my problem is solved.
But, i still don't know why this is needed.

Continue reading...
 
Back
Top