Load PNG Image from disk

Hello, I’m trying to load a image from a file in the disk and convert it to a UTexture2D but unreal keeps crashing.

I already tried to use LoadObject, but unreal keeps crashing.

Does anyone know a solution?

FString FullPath = FPlatformProcess::UserDir() + "Game/logos/countries/ad.png"
if(LoadObject<UTexture2D>(NULL, *FullPath)){

}

I wrote this for my own library not long ago. This should work for you.


UTexture2D* UImageFunctionLibrary::FileToTexture2d(FString File) {

	FString contentPath = FPaths::ProjectContentDir();
	FString fullContentPath = FPaths::ConvertRelativePathToFull(contentPath);
	FString FullPath = fullContentPath + File;
	UTexture2D* texture = nullptr;
	if (FullPath.Len() > 0) {
		FullPath.ReplaceInline(TEXT("\\"), TEXT("/"), ESearchCase::CaseSensitive);
		FullPath.ReplaceInline(TEXT("//"), TEXT("/"), ESearchCase::CaseSensitive);
		FullPath.RemoveFromStart(TEXT("/"));
		FullPath.RemoveFromEnd(TEXT("/"));
		FPlatformMisc::NormalizePath(FullPath);
		texture = FImageUtils::ImportFileAsTexture2D(FullPath);
	}
	return texture;
}
3 Likes

This worked.
Many thanks mate <3

1 Like