Skip to content

Is there a simple way to convert a Bitmap to a Compose ImageBitmap? #1246

Answered by sproctor
sproctor asked this question in Q&A
Discussion options

You must be logged in to vote

Ok, writing a painter looked complicated. I opted for what appeared to be the easiest: convert a Korim Bitmap to a Skia Bitmap.

Here's the result:

import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asComposeImageBitmap
import com.soywiz.korim.bitmap.Bitmap
import com.soywiz.korim.bitmap.Bitmap32
import org.jetbrains.skia.ColorAlphaType
import org.jetbrains.skia.ColorInfo
import org.jetbrains.skia.ColorType
import org.jetbrains.skia.ImageInfo

internal actual fun Bitmap.toComposeImageBitmap(): ImageBitmap {
    return toBMP32IfRequired().toSkiaBitmap().asComposeImageBitmap()
}

internal fun Bitmap32.toSkiaBitmap(): org.jetbrains.skia.Bitmap {
    val colorT…

Replies: 4 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@sproctor
Comment options

Comment options

You must be logged in to vote
1 reply
@sproctor
Comment options

Answer selected by soywiz
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants