HTTP/REST download images in c++

I want to get an image from a rest-server. I want to do this in a c++ function, which is callable from blueprints. Looking around here I have not really found something I think is useful.

I know there is a plugin from ufna (varest) out there, but from what I tested it doesn’t really seem to fit my needs.

Can somebody point me in the right direction, maybe I just haven’t found the right documentation page.

Here’s a quick example.

Requesting the image

TSharedRef<IHttpRequest> ThumbRequest = FHttpModule::Get().CreateRequest();
ThumbRequest->SetVerb("GET");
ThumbRequest->SetURL(args._ThumbnailUrl);
ThumbRequest->OnProcessRequestComplete().BindRaw(this, &SFeaturedImageWidget::OnThumbImageReceived);
ThumbRequest->ProcessRequest();

Receiving the Image

void SFeaturedImageWidget::OnThumbImageReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
	if (bWasSuccessful && Response.IsValid())
	{
		TArray<uint8> ImageData = Response->GetContent();

		ThumbnailBrush = CreateBrush(FName(*Request->GetURL()), ImageData);

		FButtonStyle ButtonStyle = FButtonStyle()
			.SetNormal(*ThumbnailBrush.Get())
			.SetHovered(*ThumbnailBrush.Get())
			.SetPressed(*ThumbnailBrush.Get());

		Container->ClearChildren();
		Container->AddSlot()
			[
				SNew(SBorder)
				.BorderImage(ThumbnailBrush.Get())
				.OnMouseButtonDown(this, &SFeaturedImageWidget::OnThumbClicked)
			];
	}
}

Building the brush

TSharedPtr<FSlateDynamicImageBrush> SFeaturedImageWidget::CreateBrush(FName ResourceName, TArray<uint8> ImageData)
{
	TSharedPtr<FSlateDynamicImageBrush> Brush;

	uint32 BytesPerPixel = 4;
	int32 Width = 0;
	int32 Height = 0;

	bool bSucceeded = false;
	TArray<uint8> DecodedImage;
	IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
	IImageWrapperPtr ImageWrapper = ImageWrapperModule.CreateImageWrapper(EImageFormat::PNG);

	if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(ImageData.GetData(), ImageData.Num()))
	{
		Width = ImageWrapper->GetWidth();
		Height = ImageWrapper->GetHeight();

		const TArray<uint8>* RawData = NULL;

		if (ImageWrapper->GetRaw(ERGBFormat::RGBA, 8, RawData))
		{
			DecodedImage = *RawData;
			bSucceeded = true;
		}
	}

	if (bSucceeded && FSlateApplication::Get().GetRenderer()->GenerateDynamicImageResource(ResourceName, ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), DecodedImage))
	{
		Brush = MakeShareable(new FSlateDynamicImageBrush(ResourceName, FVector2D(ImageWrapper->GetWidth(), ImageWrapper->GetHeight())));
	}

	return Brush;
}
1 Like

Requesting the image - line 4 gives me the following error:

error 2338: You cannot use raw method delegates with UObjects

Does anybody have an idea why this happens?

(btw, how does one make those fancy code snippets look so fancy?)

Solved: I changed BindRaw to BindUObject. Not tested yet, but it builds