Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

About SKIASHARP canvas.Scale

Techno_DevelopTechno_Develop Member ✭✭✭

If you have a 100x100 image,
When set to canvas.Scale(2), 200x200, which is twice the size of a 100x100 image, is displayed.

・In this case, is the coordinates of the upper left corner unchanged and is it expanded from the upper left corner?

・If you draw a circle in the center of a 100×100 image (50×50), and if you do canvas.Scale(2), the circle will be written to 100×100, but canvas.Scale If you want to display in 50 x 50 even in case of (2), is it okay to calculate with height 200/4 and width 200/4?

The explanation may be difficult to understand, but please tell me.

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    For this, you can refer to this thread: https://forums.xamarin.com/discussion/130274/how-can-i-scale-an-skbitmap-to-the-screen-size

    You can refrer the following code:

    if (resourceBitmap != null)
            {
                // Create new SKIMmageInfo object using values from canvas info object
                SKImageInfo resizeInfo = new SKImageInfo(info.Width, info.Height, info.ColorType, info.AlphaType, info.ColorSpace);
    
                // Test whether there is more room in width or height
                if (Math.Abs(resourceBitmap.Width - info.Width) > Math.Abs(resourceBitmap.Height - info.Height)){     
                    // More room in width, so leave image width set to canvas width
                    // and increase/decrease height by same ratio
                    double widthRatio = (double)info.Width / (double)resourceBitmap.Width;
                    int newHeight = (int)Math.Floor(resourceBitmap.Height * widthRatio);
    
                    resizeInfo.Height = newHeight;
                }
                else {   
                    // More room in height, so leave image height set to canvas height
                    // and increase/decrease width by same ratio                 
                    double heightRatio = (double)info.Height / (double)resourceBitmap.Height;
                    int newWidth = (int)Math.Floor(resourceBitmap.Width * heightRatio);
    
                    resizeInfo.Width = newWidth;
                }
    
                var resizedBitmap = resourceBitmap.Resize(resizeInfo, SKBitmapResizeMethod.Box);
    
                canvas.DrawBitmap(resizedBitmap,info.Width / 2 - resizedBitmap.Width / 2, info.Height / 2 - resizedBitmap.Height / 2);
            }
    
Sign In or Register to comment.