# Base64 string to Texture2D problem

**URL:** <https://forums.unrealengine.com/t/base64-string-to-texture2d-problem/479262>\
**Category:** Asset Creation\
**Tags:** convert, unreal-engine, Scripting, CPP, question\
**Created:** [February 23, 2021, 9:19am UTC](https://forums.unrealengine.com/t/base64-string-to-texture2d-problem/479262 "2021-02-23T09:19:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hexdeveloper](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hexdeveloper/32/564113_2.png) [@hexdeveloper](https://forums.unrealengine.com/u/hexdeveloper)\
**Post date:** [February 23, 2021, 9:19am UTC](https://forums.unrealengine.com/t/base64-string-to-texture2d-problem/479262/1 "2021-02-23T09:19:35Z")

</div>

/9j/4AAQSkZJRgABAQEASABIAAD/4SUsRXhpZgAASUkqAAgAAAAHAB…  
My base64 string image on load pastebin  
[https://pastebin.com/2Mb0TX69](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

[[Album] imgur.com ![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/c/2/ec29f7937a648a9e6b343d81297e34c55f071f79.jpeg "imgur.com") ](https://imgur.com/a/Oq6DjoA)

---

<div class="post-metadata">

**Author:** ![Anonymous\_5e2e6267c1d9dbda391cf4e40d20b7c7](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Anonymous\_5e2e6267c1d9dbda391cf4e40d20b7c7](https://forums.unrealengine.com/u/Anonymous_5e2e6267c1d9dbda391cf4e40d20b7c7)\
**Post date:** [March 3, 2021, 10:09am UTC](https://forums.unrealengine.com/t/base64-string-to-texture2d-problem/479262/2 "2021-03-03T10:09:24Z")

</div>

Did you ever solved this?

---

<div class="post-metadata">

**Author:** ![Anonymous\_5e2e6267c1d9dbda391cf4e40d20b7c7](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Anonymous\_5e2e6267c1d9dbda391cf4e40d20b7c7](https://forums.unrealengine.com/u/Anonymous_5e2e6267c1d9dbda391cf4e40d20b7c7)\
**Post date:** [March 3, 2021, 10:10am UTC](https://forums.unrealengine.com/t/base64-string-to-texture2d-problem/479262/3 "2021-03-03T10:10:18Z")

</div>

Did you ever solved this?

---

<div class="post-metadata">

**Author:** ![dpotuznik](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@dpotuznik](https://forums.unrealengine.com/u/dpotuznik)\
**Post date:** [July 31, 2021, 12:04pm UTC](https://forums.unrealengine.com/t/base64-string-to-texture2d-problem/479262/4 "2021-07-31T12:04:39Z")

</div>

Found it 🙂

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 🙂  
Have a nice day

Daniel

---

<div class="post-metadata">

**Author:** ![timonfreud](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/timonfreud/32/1092636_2.png) [@timonfreud](https://forums.unrealengine.com/u/timonfreud)\
**Post date:** [July 13, 2022, 6:14am UTC](https://forums.unrealengine.com/t/base64-string-to-texture2d-problem/479262/5 "2022-07-13T06:14:48Z")

</div>

Hi! [dpotuznik](https://forums.unrealengine.com/u/dpotuznik) Could you please share the full function code show me? I’m new guy to C++  
Thanks a lot

---

<div class="post-metadata">

**Author:** ![hexdeveloper](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hexdeveloper/32/564113_2.png) [@hexdeveloper](https://forums.unrealengine.com/u/hexdeveloper)\
**Post date:** [May 2, 2024, 1:40pm UTC](https://forums.unrealengine.com/t/base64-string-to-texture2d-problem/479262/6 "2024-05-02T13:40:31Z")

</div>

yes, maybe I can find time to post a ready-made solution for new versions
