How can i display an image from disk on a slate widget ?

Hi guys,

Any help on displaying an image from disk on a widget ?

I am trying this:

SNew(SImage).Image(new FSlateImageBrush(FPaths::GameContentDir() + TEXT("default_pictures/test.jpg")), FVector2D(100, 100)))

The path is correct, i can make a UTexture2D out of it and display it on a mesh. But i can’t display it in slate.

I have tried with jpg, png, bmp with no success.

Isn’t this supposed to work ?

It has to be done at runtime, i would like to allow my users to load an image from their disk and use it to decorate their ingame pawn.

Thanks

Cedric

1 Like

Hey,

You’re missing one slash in your path and try to pass it as an FName not as regular string, so it should look like this:

// note that '/' operator for strings will add them together but will also put '/' in between
FString ImagePath = FPaths::GameContentDir() / TEXT("default_pictures/test.jpg");

FName BrushName = FName(*ImagePath);
....
SNew(SImage)
.Image(new FSlateImageBrush(BrushName, FVector2D(128, 128)))
....

I’ve tested it on .png file but it should work with .jpg just fine.

Also, if you would like to have dynamic brush on SImage take a look into FSlateDynamicImageBrush class, there are some examples in UE source code.

1 Like

Hey Szyszek,

Thanks a lot, FSlateDynamicImageBrush did the trick.

I could never have FSlateImageBrush working, no idea why (even using fname and correctly formated path).

And it seems that FSlateDynamicImageBrush works only with png files.

Fortunately FSlateDynamicImageBrush provides a constructor using UTexture2D so this is what i came with at the end:

UTexture2D* NewTexture = MyFunctionThatCreatesATextureFromFile(TexturePath);
[...]
.Image(new FSlateDynamicImageBrush(NewTexture, FVector2D(128,128), FName(*TexturePath)));

And it works fine.

Again, thanks a million times, you saved me from some hours of pain today :slight_smile:

Cheers

Cedric

2 Likes