Sebastian-7280 avatar image
0 Votes"
Sebastian-7280 asked LeonLu-MSFT commented

Skia SKBitmap.GetPixel returns a different color

I have a very basic use case where I want to count the colors used on an image so I am loading the image on a SKBitmap and looping through getting each pixel color and counting in a bucket.

I get the same amount of colors the original image has (32 colors) but unfortunately I can see that the RGBA colors returned don't seem to match the colors in the image palette. The image renders just fine on a SKCanvasView but as an example, the yellow color in the sample image I'm providing comes back as a pinkish color.

Any ideas what I am missing here? Is there a further conversion I need to make on the pixel color to get the real color?


Code snippet:

 var bitmap = SKBitmap.Decode(fileStream);
 for (int row = 0; row < bitmap.Height; row++)
 for (int col = 0; col < bitmap.Width; col++)
     var pixelColor = bitmap.GetPixel(col, row); <-- this is not the right color

1958358.png (5.0 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I make a test with bitmap.GetPixel(col, row);, it return the value(`SKColor`) is RGBA not the RGB, And I found a similar thread in github:

0 Votes 0 ·

Thanks @LeonLu-MSFT for the response. I might not have expressed my issue correctly. All examples and issues I found on the Github repo usually relate to examples of bitmaps being created with SkiaSharp and then having some pixel manipulation (like SetPixel) and then getting the color from that same pixel to validate the value was set correctly based on the image color type.

The issue I am facing is based out of loading a file and then reading a pixel from it. There is no manipulation here other than the call to 'Decode' to load the file stream.

I'm starting to think that it might be related to the default color type set after decoding the image, I believe the color skew might occur because the pixel is being read based out of the image color type set which is most likely not the same color type the actual decoded image is in. I'm still investigating what the cause could be but open to more suggestions!

0 Votes 0 ·

You can use Solid color picture to make a test, if you can get the correct result.

0 Votes 0 ·

0 Answers