Base64 string to Texture2D problem

/9j/4AAQSkZJRgABAQEASABIAAD/4SUsRXhpZgAASUkqAAgAAAAHAB…
My base64 string image on load pastebin
https://pastebin.com/2Mb0TX69

This my base64 string.
I have stripped this from the line: data:image/jpeg;base64,
Perhaps I am doing something wrong. My texture is breaking and not loading correctly. Take a look at my code and explain what I am doing wrong.

UTexture2D* UMyBlueprintFunctionLibrary::Base64_ToImage(FString Source)
{
	TArray<uint8> data_buffer;
	bool isDecode = FBase64::Decode(Source, data_buffer);
	if (isDecode) {
		return CreateBitTextureAtRuntime(data_buffer);
	}
	else {
		return nullptr;
	}
}
UTexture2D* UMyBlueprintFunctionLibrary::CreateBitTextureAtRuntime(TArray<uint8>& BGRA8PixelData)
{
	const float Resolution = FGenericPlatformMath::Sqrt(BGRA8PixelData.Num() / 4);
	UTexture2D * Texture = UTexture2D::CreateTransient(Resolution, Resolution, PF_B8G8R8A8);
	FTexture2DMipMap & Mip = Texture->PlatformData->Mips[0];
	const int32 BufferSize = BGRA8PixelData.Num();
	void* MipBulkData = Mip.BulkData.Lock(LOCK_READ_WRITE);
	Mip.BulkData.Realloc(BufferSize);
	FMemory::Memcpy(MipBulkData, BGRA8PixelData.GetData(), BufferSize);
	Mip.BulkData.Unlock();
	Texture->UpdateResource();

	return Texture;
}

Image blueprint and problems

1 Like

Did you ever solved this?

Did you ever solved this?

Found it :slight_smile:

when you convert base64 data image is a byte array containing the JPG OR PNG FORMAT is not a FColor array.

you can create an texture directly by using :

FImageUtils::ImportBufferAsTexture2D(BGRA8PixelData);

and also you need send only the base64 data , so you need to extract data:image/png;base64,

UTexture2D* ATcpSocketConnection::Base64_ToImage(FString Source)
{
	TArray<uint8> data_buffer;
	FString Left, Right;
        // you need to remove Data:image....,
	Source.Split(TEXT(","),&Left,&Right);
	if (Right == "") return nullptr;
	bool isDecode = FBase64::Decode(Right, data_buffer);
	if (isDecode) {
		return CreateBitTextureAtRuntime(data_buffer);
	}
	else {
		return nullptr;
	}
}


UTexture2D* ATcpSocketConnection::CreateBitTextureAtRuntime(TArray<uint8> BGRA8PixelData)
{
	UTexture2D * Texture = FImageUtils::ImportBufferAsTexture2D(BGRA8PixelData);
	Texture->MipGenSettings = TMGS_NoMipmaps;
	Texture->CompressionSettings = TextureCompressionSettings::TC_VectorDisplacementmap;
	Texture->MipGenSettings = TextureMipGenSettings::TMGS_NoMipmaps;
	Texture->SRGB = false;
	Texture->Filter = TextureFilter::TF_Nearest;
	Texture->UpdateResource();
	return Texture;
}

hope it’s help :slight_smile:
Have a nice day

Daniel

3 Likes

Hi! dpotuznik Could you please share the full function code show me? I’m new guy to C++
Thanks a lot

1 Like