Tp323
Tp323

Reputation: 39

Problems Loading image in Kotlin Compose desktop

I'm having problems loading images in kotlin compose for desktop

It gives the following error:

image.png resource not found

I have the file on the src folder of the project

I'm not sure if the problem is with the code or the way i imported the image, or even if the problem is the still experimental nature of kotlin compose for desktop

val imageModifier = Modifier
            .height(240.dp)
            .fillMaxWidth()
            .clip(RoundedCornerShape(12.dp))

        Image(bitmap = useResource("image.png") { loadImageBitmap(it) },
            "image",
            imageModifier,
            contentScale = ContentScale.Fit)

Upvotes: 0

Views: 2094

Answers (1)

ABINASH KUMAR
ABINASH KUMAR

Reputation: 526

Keep your image file in resources folder then use it like this

val imageModifier = Modifier
            .height(240.dp)
            .fillMaxWidth()
            .clip(RoundedCornerShape(12.dp))

Image(painter = painterResource("image.png"),
      contentDescription = "image",
      imageModifier,
      contentScale = ContentScale.Fit
     )

painterResource supports raster (BMP, GIF, HEIF, ICO, JPEG, PNG, WBMP, WebP) and vector formats (SVG, XML vector drawable).

For more on this visit here https://github.com/JetBrains/compose-jb/tree/master/tutorials/Image_And_Icons_Manipulations

Upvotes: 4

Related Questions