0

I draw line using SkiaSharp as below, on an <Image>, it works fine. But when I tested in another device with another resolution, the drawing is not on the same location I saw on my first device. How can draw using SkiaSharp in a device-independent manner.

        private void canvasView_PaintSurface(object sender, SkiaSharp.Views.Forms.SKPaintSurfaceEventArgs args)
        {

            SKImageInfo info = args.Info;
            SKSurface surface = args.Surface;
            SKCanvas canvas = surface.Canvas;

            canvas.Clear();

            if (isFromSearchPage)
            {
                SKPaint thickLinePaint = new SKPaint
                {
                    Style = SKPaintStyle.Stroke, //.StrokeAndFill,
                    Color = SKColors.Yellow.WithAlpha(0x60),
                    StrokeWidth = 120//15                
                };


                int startingLine = GetFirstLineNo();//1;//26;
                int lineInPage = 0;

                if (CurrentLine >= startingLine)
                {
                    lineInPage = CurrentLine - startingLine + 1;
                }

                int positionY = (int)SepratorTypeHeight.PageHeader + (lineInPage * (int)SepratorTypeHeight.LineSeparator);

                thickLinePaint.StrokeCap = SKStrokeCap.Square;
                canvas.DrawLine(150, positionY, 1300, positionY, thickLinePaint);

                isFromSearchPage = false;
            }
        }

Rauf
  • 12,326
  • 20
  • 77
  • 126
  • What is the differerence for the devices? Is there any relationship between the line and screen size? If you wanna the line in the same position, you could try to set the location with the same percentage of screen size. – Wendy Zang - MSFT Mar 01 '21 at 06:26
  • @WendyZang-MSFT, Yes. I need in the same position. But how can I specify the percentage in canvas.DrawLine(...) method ? – Rauf Mar 01 '21 at 12:51
  • 1
    You could try tp use the converter to set the location with the percentage of screensize. Here is a example i done befor for yoru reference. https://stackoverflow.com/questions/58987878/resizing-frame-and-controls-according-to-device-size-suggestions – Wendy Zang - MSFT Mar 02 '21 at 06:29
  • @WendyZang-MSFT Hello Wendy, I need little more information regarding the percentage. For example, suppose ```positionY``` with a value 400, will mark on the fifth item of 12 items. When I check the same, on a small device the 400 will go to 8th item. – Rauf Mar 30 '21 at 14:16

0 Answers0