I have been reading up and trying to figure out how to convert an image to Base64. However, I’m a bit puzzled as to how I “should” accomplish this.
I’ve read that the EncodeBase64 and DecodeBase64 methods accept a “string” … but they don’t specify anything about binary data. Can these methods accept binary data?
From what I’ve read here in the forum, (as well as my Google searches), I am thinking that the following process “might” work??
Use a BinarySteam to open and read the image file’s data.
[code]Public Function PictureToString(p as Picture) as String
//Returns a base64 encoded version of the picture’s data.
If p=Nil Then
Return “”
End If
Return EncodeBase64(p.GetData(Picture.FormatJPEG, Picture.QualityMedium),0)
End Function
[/code]
And, if you need to go the other direction:
[code]Public Function StringToPicture(data as String) as Picture
// Convert a base64 encoded string back to a picture
Dim p As picture
If data=“” Then
Return Nil
End If
p=picture.FromData(DecodeBase64(data))
Return p
Thank you @Kimball! That makes a lot of good sense… Yeah, I just wasn’t sure exactly in regards to the process of converting binary data to the string format in Xojo.
@Kimball Larsen, if i want to resize the image to say a thumbnail and then do a base64, what do i need to do???
i don’t want to physically resize the image…
You’d need to draw the scaled image into a fresh Picture object
Dim original As Picture
Dim thumb As New Picture(WIDTH, HEIGHT [,DEPTH]) ' new blank picture, depth optional
thumb.Graphics.DrawPicture(original, 0, 0, WIDTH, HEIGHT) ' scale to WIDTH/HEIGHT