Load image from the web

Hi there,

I need help. I am new with juce and I am trying to load image into juce from base64 format used on web.

MemoryOutputStream out;
bool result = juce::Base64::convertFromBase64(out, input);
MemoryInputStream in(out.getMemoryBlock(), false);

PNGImageFormat format;

bool result1 = format.canUnderstand(in);

return format.loadFrom(in);

Can someone help me, because FileFormat cant understand data. Data is basically base64 string. Its small png image.

Haven’t tested this but assuming you have a URL to the image you want to load would something like this work?

Image getImageFromURL(String urlString)
    URL url(urlString);
    auto inStream = url.createInputStream(true);
    return ImageFileFormat::loadFrom(*inStream);

auto myImage = getImageFromURL("https://sjc3.discourse-cdn.com/business6/user_avatar/forum.juce.com/t3v4/45/4711_1.png")

After testing… no this doesn’t work! :frowning:

Actually nope, I am getting data from REST API. So inside of response image is encoded with base64 with other data. I actually succeeded to load image, but now I am struggling with getting my image inside ImageComponent so it can be used inside resized() method with FlexBoxes.


MemoryOutputStream out;
bool result = juce::Base64::convertFromBase64(out, input);
PNGImageFormat format;
return format.loadFrom(out.getData(), out.getDataSize());

Well, the obvious first thing to do would be to step into it and see what happens, and look for clues.

I actually saw something similar last week where a webserver was sending invalid base64, it was missing off the padding bytes, so the juce base64 classes were correctly refusing to parse it. It should be obvious when you debug it to see whether that’s the case for you.

Issue never was regarding not parsing base64, that step was always ok. Issue was regarding loading data into Image and getting proper Image object. ImageFormat could not undestand data.
In meanwhile I solved that issue :slight_smile: Thanks! :slight_smile: