Forum Xamarin.Mac

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Xamarin.Mac Image Resize

AdemCicekAdemCicek USMember ✭✭

Hello.
I created the Xamarin.mac cocoa application. I want to resize a photo on disk. I want to send my photo to the server. service is waiting for me in byte array type. I found a few SDKs. I was only able to run the SkiaSharp sdk. this sdk reduced the quality of the photo. Is there a sdk that I can resize with xamarin.mac cocoa app. Thank you to everyone already.

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    Try the following code

    public static NSImage MaxResizeImage(this NSImage sourceImage, float maxWidth, float maxHeight)
    {
        var sourceSize = sourceImage.Size;
        var maxResizeFactor = Math.Max(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height);
        if (maxResizeFactor > 1) return sourceImage;
        float width = (float)(maxResizeFactor * sourceSize.Width);
        float height = (float)(maxResizeFactor * sourceSize.Height);
        var targetRect = new CoreGraphics.CGRect(0,0,width, height);
        var newImage = new NSImage (new CoreGraphics.CGSize (width, height));
        newImage.LockFocus ();
        sourceImage.DrawInRect(targetRect, CoreGraphics.CGRect.Empty, NSCompositingOperation.SourceOver, 1.0f);
        newImage.UnlockFocus ();
    
        return newImage;
    }
    

    Refer https://stackoverflow.com/a/44716847/8187800

  • AdemCicekAdemCicek USMember ✭✭

    Hi colex. Thank you for the answer. I could not see the result because I could not save the NSImage resize result to a byte array or disk. Can we convert the NSImage object to byte array or save it to disk. I have not seen that this can be done with xamarin.mac. thank you.

  • ColeXColeX Member, Xamarin Team Xamurai
    edited May 2020

    You could convert the image into NSData and write into the specific path .

    Refer to the link https://stackoverflow.com/questions/3038820/how-to-save-a-nsimage-as-a-new-file .

  • AdemCicekAdemCicek USMember ✭✭

    Hello, ColeX.
    I could not understand how to apply this example. I saw it too. Will I write in view on the xcode side? Did you run this example?

  • ColeXColeX Member, Xamarin Team Xamurai

    Try

       public static void MaxResizeImage(this NSImage sourceImage, float maxWidth, float maxHeight)
        {
            var sourceSize = sourceImage.Size;
            var maxResizeFactor = Math.Max(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height);
            if (maxResizeFactor > 1) return sourceImage;
            float width = (float)(maxResizeFactor * sourceSize.Width);
            float height = (float)(maxResizeFactor * sourceSize.Height);
            var targetRect = new CoreGraphics.CGRect(0, 0, width, height);
            var newImage = new NSImage(new CoreGraphics.CGSize(width, height));
            newImage.LockFocus();
            sourceImage.DrawInRect(targetRect, CoreGraphics.CGRect.Empty, NSCompositingOperation.SourceOver, 1.0f);
            newImage.UnlockFocus();
    
            NSBitmapImageRep imageRep = newImage.Representations[0];
            NSData data = imageRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Png);
            data.Save(new NSUrl("path"), false);
        }
    
       sourceImage.MaxResizeImage(100,100);
    

    sourceImage is the photo you select from mac.

  • AdemCicekAdemCicek USMember ✭✭

    Hello ColeX. When I try the example NSBitmapImageRep imageRep = newImage.Representations [0];

    I have not seen this use. I did some research and found a function like the one below. but this example did not fail. but it didn't work. remained unresponsive.
    I'm trying to resize jpeg files. img.AsTiff () gets stuck here.

    public static Image ToImage(NSImage img)
    {
    using (var imageData = img.AsTiff())
    {
    var imgRep = NSBitmapImageRep.ImageRepFromData(imageData) as NSBitmapImageRep;
    var imageProps = new NSDictionary();
    var data = imgRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Jpeg, imageProps);
    return Image.FromStream(data.AsStream());
    }
    }

  • AdemCicekAdemCicek USMember ✭✭

    Hello again, ColeX.
    Actually, I resized with one sdk "SkiaSharp". but this sdk reduces the picture quality a lot. The 350 dpi picture becomes 96 dpi after resizing. that function is as follows. Thank you very much for your interest.
    ` public void PfImageResize(string inputPath)
    {
    int imageWidth = 2000;
    int iamgeHeight = 1333;
    int quality = 90;
    string outputPath = "";

            using (var input = File.OpenRead(inputPath))
            {
                using (var inputStream = new SKManagedStream(input))
                {
                    using (var original = SKBitmap.Decode(inputStream))
                    {
                        int width, height;
                        if (original.Width > original.Height)
                        {
                            width = imageWidth;
                            height = iamgeHeight;
                        }
                        else
                        {
                            width = iamgeHeight;
                            height = imageWidth;
                        }
    
                        using (var resized = original.Resize(new SKImageInfo(width, height), SKFilterQuality.High))
                        {
                            if (resized == null) return;
    
                            using (var image = SKImage.FromBitmap(resized))
                            {
                                using (var output = File.OpenWrite(outputPath))
                                {
                                    image.Encode(SKEncodedImageFormat.Jpeg, quality)
                                        .SaveTo(output);
                                }
                            }
                        }
                    }
                }
            }
        }`
    
Sign In or Register to comment.