Forum Libraries, Components, and Plugins


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

read a pixel info from a canvas

Hi all,

do you have an idea on how to read the colour info of a pixel in a canvas ?
I have seen on the net the reference to a "readpixel" method of SKCanvas class.
But it seems not implemented in skiasharp ?
I'm wrong ?

And, if not, any idea on how to do that ?



Best Answers


  • mattleibowmattleibow ZAXamarin Team Xamurai

    Hi Pierre,

    You are correct that SKCanvas does not yet have the ReadPixels, WritePixels and PeekPixels. Although there is no reason that it was left out in the first few releases, I haven't added it as this is not the best way to get the pixels.

    If for some reason the canvas is not backed by pixels, such as if it was backed by a SKPicture or SKDocument. In those cases, the operation would "fail".

    The better method to obtain the pixels is by the underlying surface. For example, SKImage, SKBitmap or SKSurface. These will typically succeed - unless you are requesting a format that is not supported.

    Often the canvas is obtained from a surface, so you should be able to ask the surface for the pixels. Regardless, if you do need the members available on the surface, please open an issue:

    Hope this helps...

  • pierreDurantpierreDurant USMember ✭✭
    edited April 2017

    Hi Matthew,

    Indeed, in my "DrawInSurface", I have access at the Surface.
    But what next ? There is no method to read a pixel in a surface I think ?
    What I can do I stop create an SKImage with the "snapshot" method.
    But then... I don't see any method in SKImage to read a pixel info, isn't it ?

    So, I must admit that I don't see how to do :-)
    If you have a "trick"...



    PS: for your info, I need to read the pixel info because I'm trying to write a "flood fill" function.

  • pierreDurantpierreDurant USMember ✭✭

    Yes indeed !

    Great !

    Thanks for your work Matthew !


  • pierreDurantpierreDurant USMember ✭✭


    The documentation is not up-to-date. Can you explain in a few words how to use this method ?
    How to define the destination buffer ?



  • pierreDurantpierreDurant USMember ✭✭
    edited April 2017

    I made some tests but no result....
    in my DrawInSurface method:

    public override void DrawInSurface (SKSurface surface, SKImageInfo info)
        base.DrawInSurface (surface, info);
        SKImageInfo dstinf = new SKImageInfo ();
        dstinf.ColorType = info.ColorType;
        dstinf.AlphaType = info.AlphaType;
        dstinf.Width = 1;
        dstinf.Height = 1;
        IntPtr dstpixels = IntPtr.Zero; 
        SKCanvas canvas = surface.Canvas;
        using (var paint = new SKPaint ()) {
            paint.TextSize = 64.0f;
            paint.IsAntialias = true;
            paint.Color = SKColors.Red;
            paint.IsStroke = false;
            SKRect rect = new SKRect (0.0f, 70.0f, 100.0f, 100.0f);
            canvas.DrawRect (rect, paint);
        bool test;
        test = surface.ReadPixels (dstinf, dstpixels, 4 , 50, 80);

    Result: test is always false.....
    what's wrong ?
    If you have some lines as example....


  • pierreDurantpierreDurant USMember ✭✭

    Yes it works !!!
    You are the best !

    Thanks a lot !

Sign In or Register to comment.