Compressione delle dimensioni dell'immagine selezionate Xamarin prima del caricamento nel cloud

Compressione delle dimensioni dell'immagine selezionate Xamarin prima del caricamento nel cloud

La soluzione per la compressione delle dimensioni dell'immagine selezionate Xamarin prima del caricamento nel cloud
è indicata di seguito:

Sto lavorando su un'app Xamarin in cui gli utenti possono selezionare immagini utilizzando il plug-in Xamarin Essentials. Il mio problema ora è trovare il modo di ridurre le dimensioni dell'immagine utilizzando il percorso completo dell'immagine, prima che venga caricata nel cloud.

Il codice

// Pick Image
    private async Task PickImages()
    {
        if (ImageCollection.Count >= 10)
        {
            ToastMessageLong("Cannot Select More then 10 Images.");
            return;
        }
        ImageLink image = new();
        try
        {
            FileResult result = await MediaPicker.PickPhotoAsync(new MediaPickerOptions
            {
                Title = "Pick an Image"
            });

            if (result == null) return;

            image.PostImages = result.FullPath;
            ImageCollection.Add(image);
        }
        catch (Exception x)
        {
            await DisplayAlert("", x.Message);
        }
    }


    private async Task UploadImagesToCloud()
    {
        if (ImageCollection.Count > 0)
        {
            List<ImageLink> imageLinks = new();
            foreach (ImageLink img in ImageCollection)
            {
                // Need to Compress Image before adding to cloud..

                ImageLink link = await CloudService.CS.UploadPostImage(img.PostImages);
                imageLinks.Add(link);
            }
            P.Images = imageLinks;
        }
    }

Puoi ridimensionare le dimensioni dell'immagine prima di caricarla sul cloud. Ecco un metodo per ridimensionare l'immagine:

#if __IOS__
    public static byte[] ResizeImageIOS(byte[] imageData, float width, float height)
    {
        UIImage originalImage = ImageFromByteArray(imageData);
        UIImageOrientation orientation = originalImage.Orientation;

        //create a 24bit RGB image
        using (CGBitmapContext context = new CGBitmapContext(IntPtr.Zero,
                                             (int)width, (int)height, 8,
                                             4 * (int)width, CGColorSpace.CreateDeviceRGB(),
                                             CGImageAlphaInfo.PremultipliedFirst))
        {

            RectangleF imageRect = new RectangleF(0, 0, width, height);

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

            UIKit.UIImage resizedImage = UIKit.UIImage.FromImage(context.ToImage(), 0, orientation);

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


#if __ANDROID__
    
    public static byte[] ResizeImageAndroid (byte[] imageData, float width, float height)
    {
        // Load the bitmap
        Bitmap originalImage = BitmapFactory.DecodeByteArray (imageData, 0, imageData.Length);
        Bitmap resizedImage = Bitmap.CreateScaledBitmap(originalImage, (int)width, (int)height, false);

        using (MemoryStream ms = new MemoryStream())
        {
            resizedImage.Compress (Bitmap.CompressFormat.Jpeg, 100, ms);
            return ms.ToArray ();
        }
    }

potresti fare riferimento a ImageResizer

Soluzione che mi è stata data utilizzando SkiaSharp.

    public static string CreateThumbnail(string Path, string fileName)
    {
        var bitmap = SKBitmap.Decode(Path);
        int h = bitmap.Height;
        int w = bitmap.Width;
        int newWidth = w;
        int newHeight = h;
        //resize algorythm
        if (h > 1080 || w > 1080)
        {
            int rectHeight = 1080;
            int rectWidth = 1080;

            //aspect ratio calculation
            float W = w;
            float H = h;
            float aspect = W / H;

            //new dimensions by aspect ratio
            newWidth = (int)(rectWidth * aspect);
            newHeight = (int)(newWidth / aspect);

            //if one of the two dimensions exceed the box dimensions
            if (newWidth > rectWidth || newHeight > rectHeight)
            {
                //depending on which of the two exceeds the box dimensions set it as the box dimension and calculate the other one based on the aspect ratio
                if (newWidth > newHeight)
                {
                    newWidth = rectWidth;
                    newHeight = (int)(newWidth / aspect);
                }
                else
                {
                    newHeight = rectHeight;
                    newWidth = (int)(newHeight * aspect);
                }
            }
        }

        var resizedImage = bitmap.Resize(new SKImageInfo(newWidth, newHeight), SKBitmapResizeMethod.Lanczos3);
        var image = resizedImage.Encode(SKEncodedImageFormat.Jpeg, 80);
        var path = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
        var filepath = System.IO.Path.Combine(path, fileName);
        string finalPath = filepath;
        using (var stream = File.OpenWrite(filepath))
            image.SaveTo(stream);
        return finalPath;
    }