locked
Resizing a skiasharp canvas in order to print to a Pdf RRS feed

  • Question

  • User379462 posted

    Trying to make use of skiasharp implementation in a xamarin UWP-only project, which requires a bit of scaling and printing to a pdf with the end result. So far, results have been encouraging however printing to Pdf by means of cropping the bitmap failed miserably. This is the current implementation, would hope for some advice on how to best resize/scale/crop in order to have the pdf look decent:

    public SKBitmap CroppedBitmap(SKBitmap bitmap) { float desiredWidth = 1096; float desiredHeight = 700;

            float left = ((bitmap.Width - desiredWidth) / 2) + 2;
            float top = ((bitmap.Height - desiredHeight) / 2) - 4;
            float right = left + desiredWidth;
            float bottom = top + desiredHeight;
    
            //SKRect cropRect = croppingRect.Rect;
            SKRect cropRect = new SKRect(left, top, right, bottom);
            SKBitmap croppedBitmap = new SKBitmap((int)cropRect.Width,
                                                    (int)cropRect.Height);
            SKRect dest = new SKRect(0, 0, cropRect.Width, cropRect.Height);
            source = new SKRect(cropRect.Left, cropRect.Top,
                                        cropRect.Right, cropRect.Bottom);
    
            using (SKCanvas canvas = new SKCanvas(croppedBitmap))
            {
                canvas.DrawBitmap(bitmap, source, dest);
            }
    
            return croppedBitmap;         
        }
    
    Tuesday, June 18, 2019 4:32 PM

Answers

  • User342125 posted

    Are you trying to scale your bitmap down to the desired PDF resolution? If so you need to remember to maintain the aspect ratio of the original bitmap. If you dont then it will appear stretched on one of the axis. The code below is a quick (and untested) modification of some code I had to do this in one of my projects. It calculates the new scale, calculates the required height/width to maintain the aspect ratio and draws to the centre of the desired output. Fingers crossed it should be easy for you to integrate it with your existing code

    public SKBitmap ScaledBitmap(SKBitmap bitmap)
    {
                // Calculate the scale
                    // Maintain aspect ratio
                    double scale = 1.0;
                    double height =desiredHeight;
                    double width = desiredWidth;
    
                    // Which is scaled more, height or width?
                    if (desiredWidth / bitmap.Width < desiredHeight / bitmap.Height)
                    {
                        scale = desiredWidth / bitmap.Width;
                        height = desiredHeight * scale;
                    }
                    else
                    {
                        scale = (double)desiredHeight / bitmap.Height;
                        width = (double)desiredWidth * scale;
                    }
    
                    Point centreoffset = new Point((int)(desiredWidth - width) / 2, (int)(desiredHeight - height) / 2);
    
                SKBitmap croppedBitmap = new SKBitmap(desiredWidth, desiredHeight);
                    using (SKCanvas canvas = new SKCanvas(croppedBitmap))
                    {
                        // Draw back buffer
                        canvas.Clear(SKColors.Transparent);
                        canvas .DrawBitmap(bitmap, new SKRect(0, 0, bitmap.Width, bitmap.Height), new SKRect(centreoffset.X, centreoffset.Y, centreoffset.X + (int)width, centreoffset.Y + (int)height));
                    }
    
                     return croppedbitmap
    }
    

    HTH Paul

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, June 19, 2019 9:58 AM