Dynamic Load BMP from file, blank texture.

Hey,

I’m attempting to dynamically load an image from file and display it on my model using a dynamic material.

All the dynamic material stuff seems to be working fine (as when I change the texture parameter value, it goes blank)

Though I seem to be loading in my bmp incorrectly from the file system.

The code I’m using to do so is:

UTexture2D* getTexture(FString filename) {

	TArray<uint8> RawFileData;
	UTexture2D* mytex = nullptr;

	IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
	// Note: PNG format.  Other formats are supported
	IImageWrapperPtr ImageWrapper = ImageWrapperModule.CreateImageWrapper(EImageFormat::BMP);

	if (FFileHelper::LoadFileToArray(RawFileData, *filename))
	{
		if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(RawFileData.GetData(), RawFileData.Num()))
		{
			const TArray<uint8>* UncompressedBGRA = NULL;
			if (ImageWrapper->GetRaw(ERGBFormat::RGBA,8, UncompressedBGRA))
			{
				mytex = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_R8G8B8A8);
			}
		}
	}

	return mytex;
}

When I inspect mytex, the width and height of the texture is correct but when applied to the dynamic material renders only white.

Anyone have any ideas?

Any help would be definitely appreciated :slight_smile:

You did not copy anything to your newly created texture (mytex), you need to get the mip 0 buffer, lock it, fill them with the colors from your BMP file, unlock it and then update the texture resource so that it is marked as needs to be uploaded to the video memory.

This is the kinda things that you’re missing (I copy paste this from Rama’s post in the forum) :

	//Create T2D!
	if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(RawFileData.GetData(), RawFileData.Num()))
	{ 
		const TArray<uint8>* UncompressedBGRA = NULL;
		if (ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, UncompressedBGRA))
		{
			LoadedT2D = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_B8G8R8A8);
			
			//Valid?
			if (!LoadedT2D) 
			{
				return NULL;
			}
			
			//Out!
			Width = ImageWrapper->GetWidth();
			Height = ImageWrapper->GetHeight();
			 
			//Copy!
			void* TextureData = LoadedT2D->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
			FMemory::Memcpy(TextureData, UncompressedBGRA->GetData(), UncompressedBGRA->Num());
			LoadedT2D->PlatformData->Mips[0].BulkData.Unlock();

			//Update!
			LoadedT2D->UpdateResource();
		}
	}

You can read the full post here:

I had found the answer myself shortly after posting this. Came back to make sure to mark this question as correct and say thanks :slight_smile: