Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Get bitmap touch coordinates after pan and scale using SkiaSharp

lrgsclrgsc Member ✭✭

I have been playing around with the SkiaSharpFormsDemos and implemented the Basics bitmap rendering and touch recognisers on my project. What I can't figure out now is, after the bitmap has been panned or scaled and when a touch is made, translate the X,Y coordinates of the touch to the actual X,Y coordinates on the bitmap.

After reading about it more, I have found that you should be able to "reverse" the pan and scale done to the bitmap by using the Matrix.TransX-Y and Matrix.ScaleX-Y and with the touch coordinates to get the actual bitmap coordinates like so:

        double bitmapX = (touchPoint.X - bitmap.Matrix.TransX) / bitmap.Matrix.ScaleX;
        double bitmapY = (touchPoint.Y - bitmap.Matrix.TransY) / bitmap.Matrix.ScaleY;

But the points I get are way off when I am comparing it to the original bitmap. Is there something I am misunderstanding?
My goal is to capture double touches on the bitmap (done!) and then paint a marker on the bitmap where the double touch was made (not done!). The marker must stay put even after the bitmap was panned or scaled so I need to know where to draw on the bitmap. The bitmaps can be large images that wont fit most screens so the need for panning and scaling.

Best Answer

  • lrgsclrgsc Member ✭✭
    Accepted Answer

    Figured it out. Finding the right bitmap coordinates using the process above was correct. What was wrong was the image was being scaled to the device screen resolution, hence messing up the coordinates. All I need to set on the SKCanvasView was:
    IgnorePixelScaling="True"
    After that, the coordinates were perfectly aligned even after pan and scaling.

Answers

  • lrgsclrgsc Member ✭✭
    Accepted Answer

    Figured it out. Finding the right bitmap coordinates using the process above was correct. What was wrong was the image was being scaled to the device screen resolution, hence messing up the coordinates. All I need to set on the SKCanvasView was:
    IgnorePixelScaling="True"
    After that, the coordinates were perfectly aligned even after pan and scaling.

Sign In or Register to comment.