I'm new at using Mapsui/SkiaSharp so forgive me if this is obvious.
I'm trying to print! A map is rendered to an image using Mapsui using the "Render" method below (calls Mapsui.Rendering.Skia.MapRenderer) which takes a map, the world extents in Spherical Mercator, and the size of the image (or window). This works fine for screen resolutions.
When I print, because the DPI is higher for the printer, the on-map text is tiny and unreadable.
It may be my inexperience, but I know Skia is accomplished at cross platform rendering for device independence. Mapsui also has a specific section in the documentation about scaling but it references using SKCanvasView.IgnorePixelScaling which I don't have access to via the MapRenderer which only acts on a SKCanvas.
Is there something I am missing about rendering for printing?
Is there a printing specific rendering context or pipeline I can use that automatically scales the fonts appropriately, rather than writing the bitmap to the GDI Graphics context?
(code below is the OnPrintPage override from PrintDocument).
/// <summary>
/// Do the printing
/// </summary>
/// <param name="e"></param>
protected override void OnPrintPage(PrintPageEventArgs e)
{
try
{
// divide by 100 because e.PageBounds is inches times 100
Size size = new Size(
e.PageBounds.Width * (int)(e.Graphics.DpiX / 100.0f),
e.PageBounds.Height * (int)(e.Graphics.DpiY / 100.0f));
// draw the bitmap to the graphics context
using (var image = Renderer.Render(Map, Extents, size))
e.Graphics.DrawImage(image, 0, 0, e.PageBounds.Width, e.PageBounds.Height);
}
catch (Exception ex)
{
Trace.TraceError("MapReportPrintDocument.OnPrintPage: Error printing document\r\n{0}", ex);
}
}
Edited to add more details; One method of inquiry that's given me some results is the following: rendering to an XpsDocument creates a context where I can let the document context know the DPI, and it automatically adjusts the scaling of the fonts for me. This is not ideal as now I need to manipulate the XPS file to get it printed and there's some wrinkles with that.
public string RenderToXps(IMap map, Extents extents, SizeF size, float dpi = 72)
{
var resolution = Mapsui.Utilities.ZoomHelper.DetermineResolution(extents.Width, extents.Height, size.Width, size.Height);
var viewport = new Mapsui.Viewport()
{
Center = extents.Center.ToMapsui(),
Resolution = resolution,
Width = size.Width,
Height = size.Height
};
var msMap = map.GetMapsuiMap();
var path = Path.GetTempFileName();
using (var stream = new SkiaSharp.SKFileWStream(path))
{
using (var document = SkiaSharp.SKDocument.CreateXps(stream, dpi))
{
var canvas = document.BeginPage(size.Width, size.Height);
Renderer.Render(canvas, viewport, msMap.Layers, msMap.Widgets);
document.EndPage();
}
}
return path;
}