SkiaSharp icon indicating copy to clipboard operation
SkiaSharp copied to clipboard

[BUG] XPS DrawBitMap is clipping?

Open Jonesie opened this issue 3 years ago • 5 comments

Description

Im using SkiaSharp to create an XPS (and/or PDF) document that includes images for printing. Images in PDF are correctly stretched to fit the required rect, but on XPS they seem to be clipped. My code and sample images are below.

Is this a bug or am I doing something wrong? I would have thought that the code for PDF should be identical for XPS but maybe there's extra trick or hack I need to do?

Code

public void ToDocument(string inputPath, bool pdf = false)
        {

            SKDocument document = null;
            var outputPath = Path.Combine(Path.GetDirectoryName(inputPath), Path.GetFileNameWithoutExtension(inputPath)) + (pdf ? ".pdf" : ".xps");

            using var input = File.OpenRead(inputPath);
            using var inputStream = new SKManagedStream(input);
            using var original = SKBitmap.Decode(inputStream);

            // page a little bigger than source image
            var pageRect = new SKRect(0, 0, original.Width + 20, original.Height + 100);

            var renderRect = new SKRect(0, 0,
                (float)original.Width, (float)original.Height);

            using var outputStream = File.Create(outputPath);
            using var managedStream = new SKManagedWStream(outputStream);

            // null checking removed for readability

            using var canvas = document.BeginPage(pageRect.Width, pageRect.Height);
            canvas.Clear();

            using var p = new SKPaint();

            SKRect picRect;
            float xFactor = 0.25F;
            float yFactor = 0.25F;
            if (pdf)
            {
                // stretch renderRect for pdf
                picRect = new SKRect(renderRect.Left, renderRect.Top, renderRect.Left + renderRect.Width * xFactor, renderRect.Top + renderRect.Height * yFactor);
            }
            else
            {
                // stretch renderRect for xps
                // xFactor = 1.25F;
                // yFactor = 1.25F;
                picRect = new SKRect(renderRect.Left, renderRect.Top, renderRect.Left + renderRect.Width * xFactor, renderRect.Top + renderRect.Height * yFactor);
            }
            canvas.DrawBitmap(original, original.Info.Rect, picRect, p);

            p.StrokeWidth = 2;
            p.Color = SKColors.Red;
            p.Style = SKPaintStyle.Stroke;

            // rect same size as original bitmap
            canvas.DrawRect(renderRect, p);

            p.Color = SKColors.Blue;
            // rect same size as stretched bitmap
            canvas.DrawRect(picRect, p);

            using var tp = new SKPaint();
            tp.TextSize = 10.0f;
            tp.IsAntialias = true;
            tp.IsStroke = false;
            //canvas.DrawText($"Page: {pageRect.Height}w {pageRect.Width}h", 10F, pageRect.Bottom - 55F, tp);
            tp.Color = SKColors.Red;
            canvas.DrawText($"Source: {original.Width}w {original.Height}h", 10F, pageRect.Bottom - 40F, tp);
            tp.Color = SKColors.Blue;
            canvas.DrawText($"Dest: {picRect.Width}w {picRect.Height}h", 10F, pageRect.Bottom - 25F, tp);
            canvas.DrawText($"Stretched: {xFactor}w {yFactor}h", 10F, pageRect.Bottom - 10F, tp);


            // bitmap should fit to the background rect but fails with xps
            //canvas.DrawBitmap(original, renderRect, p);

            document.Close();
        }

Expected Behavior

Actual Behavior

Some images are clipped - it seems like small ones are ok. Maybe there is a buffer size issue or ... ?

Basic Information

  • Version with issue: 2.80.2
  • Last known good version: ??
  • IDE: Visual Studio, Visual Studio Code
  • Platform Target Frameworks:
    • Linux: Ubuntu 16.04 (AWS Linux)
    • Windows Classic: 10
  • Target Devices:
    • n/a - printing the xps
Detailed IDE/OS information (click to expand)

PASTE ANY DETAILED VERSION INFO HERE

Screenshots Original image: Original

PDF: PDF

XPS: XPS

Jonesie avatar Jun 24 '21 22:06 Jonesie