Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Resize Images with same aspect ratio and Quality

$
0
0

I am trying to resize images.While I was able to maintain the same aspect ratio but the quality degrades.I am using PCL and resizing the images in a native way.

`
UIImage sourceImage = ImageFromByteArray (imageData);
CGSize imageSize = sourceImage.Size;
double scaleFactor = 0.00;
CGSize targetSize = new CGSize ();
targetSize.Height = tileHeight;
targetSize.Width = tileWidth;

        double width = Convert.ToDouble(imageSize.Width);
        double height = Convert.ToDouble(imageSize.Height);

        double targetWidth = tileWidth;
        double targetHeight = tileHeight;

        //float scaleFactor = 0.0;
        double scaledWidth = targetWidth;
        double scaledHeight = targetHeight;



            double widthFactor = targetWidth / width;
            double heightFactor = targetHeight / height;

            if (widthFactor < heightFactor) 
                scaleFactor = widthFactor;
            else
                scaleFactor = heightFactor;

        scaledWidth  = width * scaleFactor;
        scaledHeight = height * scaleFactor;    



        int w = Convert.ToInt32(scaledWidth);
        int h = Convert.ToInt32(scaledHeight);
        int bytesPerRow = Convert.ToInt32((4 * scaledWidth));

        //reate a 24bit RGB image
        using (CGBitmapContext context = new CGBitmapContext (IntPtr.Zero,
            w, h, 8,
            bytesPerRow, CGColorSpace.CreateDeviceRGB (),
            CGImageAlphaInfo.PremultipliedFirst)) 
        {

            RectangleF imageRect = new RectangleF (0, 0, (float)scaledWidth, (float)scaledHeight);

                        // draw the image
            context.DrawImage (imageRect, sourceImage.CGImage);

                        UIKit.UIImage resizedImage = UIKit.UIImage.FromImage (context.ToImage ());

                        // save the image as a jpeg
                        return resizedImage.AsJPEG ().ToArray ();
                    }`

Can someone help me with this?I am basically an android guy and has implemented this using the sample code.


Viewing all articles
Browse latest Browse all 91519

Trending Articles



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