Device Independent Drawing

Rauf Muhammed 21 Reputation points
2021-03-30T14:03:05.167+00:00

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;
            }
        }
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,377 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 81,191 Reputation points Microsoft External Staff
    2021-03-31T06:46:07.083+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    When you draw a line that need to suitable for different resolution, please do not set the specific value when you drawLine.

    Specific value should be interpreted as SKImageInfo.Width or Height's proportional.

    such as

       float xText = info.Width / 2;  
                   float xLine1 = info.Width/ 10;  
                   float xLine2 = info.Width - xLine1;  
                   float y = thickLinePaint.FontSpacing;  
         
                   thickLinePaint.StrokeCap = SKStrokeCap.Square;  
                //   canvas.DrawLine(150, positionY, 1300, positionY, thickLinePaint);  
                   canvas.DrawLine(xLine1, y, xLine2, y, thickLinePaint);  
    

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.