How to download jpg file from web server?

Hi guys,

I want to download jpg image from web server, but can't find the effective method at current. Could you tell me one method to do this? Thanks.

Best regards

The URL class would be the place to look. Just download the data and use ImageFileFormat::loadFrom

Hey I’m trying to get this working in an application but can’t seem to find the right combo. It’s downloading data to the memory stream properly but the image parsing seems to be failing. Here’s my code:

		URL imageLink("https://www.catster.com/wp-content/uploads/2018/07/Savannah-cat-long-body-shot.jpg");
		MemoryBlock memoryBlock;
		if(imageLink.readEntireBinaryStream(memoryBlock))
		{
			Image buttonImage = ImageFileFormat::loadFrom((void*)&memoryBlock, memoryBlock.getSize());
			int buttonWidth = buttonImage.getWidth();
			
			button.setImages(true, true, false, buttonImage, 1.0, Colours::red, buttonImage, 0.5, Colours::red, buttonImage, 1.0, Colours::red);
		}

buttonWidth is always zero because it returns a blank image. The part that doesn’t pass is this check traced down from ImageFileFormat::loadFrom:

ImageFileFormat* format = findImageFormatForStream (input)

I thought it might be something with the image format but it doesn’t seem to work with any url I try. Is there something else that needs to happen to get it into a format that the ImageFileFormat will accept?

Thanks!

I stopped reading here, since this is not how you access the data. This kind of try and error casting will get you into trouble :wink:

Try this instead:

Image buttonImage = ImageFileFormat::loadFrom (memoryBlock.getData(), memoryBlock.getSize());
1 Like

That’s the ticket! Blarg, stupid mistake. Thanks for the help and fast response. Here is the full example for anyone trying to do the same in the future:

		URL imageLink("https://www.catster.com/wp-content/uploads/2018/07/Savannah-cat-long-body-shot.jpg");
		MemoryBlock memoryBlock;
		if(imageLink.readEntireBinaryStream(memoryBlock))
		{
			Image buttonImage = ImageFileFormat::loadFrom(memoryBlock.getData(), memoryBlock.getSize());
			int buttonWidth = buttonImage.getWidth();
			
			button.setImages(true, true, false, buttonImage, 1.0, Colours::transparentBlack, buttonImage, 0.5, Colours::transparentBlack, buttonImage, 1.0, Colours::transparentBlack);
		}
2 Likes