Load Base 64 String into a Picture Box

Hoping someone can help, I’m going around and around in circles trying to get this to work.

I’m retrieving a Base 64 Image from my database, I know the string is correct because I can use online decoders to correctly see the picture.

I’ve tried many things including but not limited to what I’ve found here:

https://discuss.appstudio.dev/t/load-base64-to-image/793

https://wiki.appstudio.dev/PictureBox

https://blog.appstudio.dev/2011/03/images-in-strings-and-base64/

According to the docs, I should be able to just use picb.addImage(s) where picb is my picture box and s is the base 64 string. Looking at the first link, I’m not sure if I need to decode the string or not - I’ve tried both ways with no luck.

Any suggestions?

1 Like

Do you have a sample of the base64 string / file? It could be there are special characters being embedded that prevent it from being streamed.

base64img.zip (79.7 KB)

I use Convert Base64 to JPEG – Online JPG Tools to view this file.

I tried your string in both an Image and a PictureBox:

1 Like

Any chance you could show me exactly what code you are using? I’ve tried about 6 different ways, and it’s just not working. Not sure where I’m going wrong.

If req.status = 200 Then
  r = req.responseText
    'MsgBox(r, vbOKCancel)
 pbScore.getContext("2d")
 pbScore.addImage(r)
  
Else
    MsgBox ("ERROR:  Refresh couldn't reach the server")
End If

This is what I have so far, and it’s not working. If I uncomment the MsgBox I do get the full base 64 string required.

If you are downloading the image then the base 64 string may not be fully downloaded just yet. Add a pause or a way to ensure it has fully downloaded. Try a smaller file size and see.

It’s fully downloaded - I can send the string to a msgbox or print it, then copy and paste it to the website I mentioned above and it decodes fine.

I should mention, this is stored in a database, I’m accessing the string via a php script and ajax. So there should be no way I’m getting partial data.

Ah yes…you did. Hmmmm…can you test out the string by hard coding the base64 string to the r variable? If that works it could be the string maybe introducing characters that are stripped out when passed to the msg box.

That was my next idea. I’ll give it a go when I’m back from work tonight.

I put the string into a file called base64img.js like this:

x = "data:image/png;base64,iV..."

I then dragged and dropped the file into a copy of the Base64Image sample’s folder.

I made one change to the code in Form1:

reddot = x

@John73 hopefully that’s it.

.

So after much mucking about, and having not touched this code in the last 6 weeks, I’ve finally got it working - thanks to everyone that helped.

The final solution, and I still don’t really understand the under the hood workings was adding an ImageBox and then using the following code (ignore the req.status etc if you aren’t obtaining your picture data via an Ajax call):

   If req.status = 200 Then
        r = req.responseText
        Image1.innerHTML = "<img src='" & r & "'>"
        pb = pbScore.getContext("2d")
        pb.addImage(r)
    Else
        MsgBox ("ERROR:  GetPicture couldn't reach the server")
    End If

You probably don’t need this line:

Image1.innerHTML = "<img src='" & r & "'>"

(Unless you have a separate control called Image1 which you also want to display the image in)