Komprimieren der ausgewählten Bildgröße Xamarin vor dem Hochladen in die Cloud

Komprimieren der ausgewählten Bildgröße Xamarin vor dem Hochladen in die Cloud

Lösung zum Komprimieren der ausgewählten Bildgröße Xamarin vor dem Hochladen in die Cloud
ist unten angegeben:

Ich arbeite an einer Xamarin-App, in der Benutzer Bilder mit dem Xamarin Essentials-Plugin auswählen können. Mein Problem besteht jetzt darin, die Bildgröße mithilfe des vollständigen Pfads des Bilds zu verkleinern, bevor es in die Cloud geladen wird.

Der Kodex

// 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;
        }
    }

Sie können die Bildgröße ändern, bevor Sie es in die Cloud hochladen. Hier ist eine Methode zum Ändern der Bildgröße:

#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 ();
        }
    }

Sie könnten sich auf ImageResizer beziehen

Lösung, die ich mit SkiaSharp erhalten habe.

    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;
    }