Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

How to draw using real sizes on Xamarin.Forms using SkiaSharp?

$
0
0

Hi. My goal is to draw a line in a fixed size in millimeters
I have tried to achieve this by converting from mm to dp. I'm using this formula:

private void canvasView_PaintSurface(object sender, SKPaintSurfaceEventArgs e)
{
    SKSurface surface = e.Surface;
    SKCanvas canvas = surface.Canvas;
    canvas.Clear(SKColors.CornflowerBlue);

    int width = e.Info.Width;

    //Set transforms
    canvas.Translate(width / 2, 30);




    // In milimeters
    float heightInMilimeters = 10f;

    // In pixels.
    // Constants.PIXELS_IN_MILIMETER = 3.779527559f
    var heightInPixels = heightInMilimeters * Constants.PIXELS_IN_MILIMETER;

    // In dp
    //dp = px / (dpi / 160)

    // heightInPixels = 37.79527559
    var heightInDeviceUnit = heightInPixels / 1.5   // Device with hdpi

    // heightInDeviceUnit = 25.19685039333333
    var rect = SKRect.Create(widthInDeviceUnit, heightInDeviceUnit);
    canvas.DrawRect(rect, blackFillPAint);
}

But when I measure the rectangle with a ruler it is not 10mm height.

I'm not sure if the way I went is the right way and if I missing something at the road.
Any help will be apreciated.
Thanks


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>