Find a pixel of given color in a bitmap


I'm making a color picker using SkiaSharp, and so far so good. I can load a bitmap with a color gradient and get the selected color using the following (where _sampleBitmapImageInfo is a 1x1 imageinfo in the format of my gradient bitmap):

    private SKColor getPixelAtPoint(SKSurface surface, int x, int y)
        var bitmap = new SKBitmap(_sampleBitmapImageInfo);

        // get the pixel buffer for the bitmap
        var pixels = bitmap.GetPixels();

        // read the surface into the bitmap
        surface.ReadPixels(_sampleBitmapImageInfo, pixels, _sampleBitmapImageInfo.RowBytes, x, y);

        // access the color
        var color = bitmap.GetPixel(0, 0);

        return color;

You'll see from the comments that this was taken from another post on this forum, and it works great.

Additionally, as the user drags their finger around the color picker, it draws a little circle indicator to show what color they have selected. Again, works great.

My problem is that when I'm loading the color picker back, I want to draw that circle indicator at the location of a specified color. So if the user picked Red (255, 0, 0), I'd like to be able to find the first Red (255, 0, 0) pixel in the bitmap.

I've tried using the above getPixelAtPoint method while iterating over the entire bitmap's width / height and it never finds the color, even though it was a color that was just selected using the same bitmap. Additionally, it runs painfully slow (which makes sense). I just have a feeling that there has to be better way.


Sign In or Register to comment.