Home > Net >  Problems Loading image in Kotlin Compose desktop
Problems Loading image in Kotlin Compose desktop

Time:09-22

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)

CodePudding user response:

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

  • Related