How to load a Base64 image to TextureRect

Godot Version

4.2.1

Question

I am trying to load a base64 string to TextureRect but it is not displaying. My base64 string is correct and i have validated it online.

Below is my code


extends Node2D


@onready var texturerect = $TextureRect

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	var img = Image.new()
	
	
	img.load_jpg_from_buffer(
	Marshalls.base64_to_raw("my_base_64_string")
)
	var texture = ImageTexture.new()
	texture.create_from_image(img)

Always read the warnings!

The function “create_from_image()” is a static function but was called from an instance. Instead, it should be directly called from the type

So here’s the working version:

extends Node2D

@onready var texturerect = $TextureRect

func _ready() -> void:
	var img = Image.new()

	img.load_jpg_from_buffer(
		Marshalls.base64_to_raw("base_64_string")
	)

	texturerect.texture = ImageTexture.create_from_image(img)
1 Like