Resize images and save thumbnails

Hello.
I've tried to google around for a c# sample that could explain how to resize image files to thumbnails (thus keeping aspect ratio).
Howvere I ran into objective-c-only code snippets which are not clear to me.

Is there any sample code that does the trick? All I found is relevant to android only, no ios....

Thanks.
Alberto.

Posts

  • ChrisHonselaarChrisHonselaar NLBeta ✭✭✭

    You might also like to try ProtoPad, it allows you to very quickly experiment with stuff like this.

  • AlbertoPastoreAlbertoPastore ITMember

    Great! I've been trying both your code suggestion and ProtoPad, I'm pretty new to mobile developement, but coming from windows .net experience.
    Xamarin Rocks! I've been making very quick progress in just 3 days, now I'm at least able to compile my first universal app (with something more than just a hello world label!!! :)

    I'm doing my own photo gallery... it works like a charm with collections views.

    Thank you

  • AlbertoPastoreAlbertoPastore ITMember

    Thanks Jason, I've already figured it out myself, and forgot the post a comment, "min" makes much more sense!

  • AlbertoPastoreAlbertoPastore ITMember

    After experimenting on different pieces of code, I decide to move from UI to CG.
    It looks like the UIGraphics.BeginImageContext() ... UIGraphics.EndImageContext() is suffering from some sort of memory leakage.

    I'm using the resizing function to generate several thumbnails, and my version of the function has been carefully modified to properly dispose any disposable object via using().

    However the app crashes regularly after about 400 images being resized, having only 30 Mb ram left on the iPad and after several DidReceiveMemoryWarning() events (even tried to GC.collect() every 10-20 thumbnails with no effect at all).

    I think something's wrong with UIkit implementation....

    Also found a similar issue in this post on stack overflow:

    stackoverflow.com/questions/8396698/monotouch-memory-leak-when-drawing-pdf-on-a-custom-graphics-context

  • XamTwoXamTwo USMember

    Thanks SO MUCH for the methods! Extremely helpful!

  • AllisterAllister USMember ✭✭
  • syedaliazizsyedaliaziz USMember ✭✭
    edited September 2015

    @ChrisHonselaar Thanks for the functions - quite helpful.

    Can you also point to Android and WP specific implementation of these resizing and cropping methods?

  • JalesCJalesC BRMember

    Update this code

    public static class ExtensionMethods
    {
        // resize the image to be contained within a maximum width and height, keeping aspect ratio
        public static UIImage MaxResizeImage(this UIImage sourceImage, float maxWidth, float maxHeight)
        {
            var sourceSize = sourceImage.Size;
            var maxResizeFactor = Math.Max(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height);
            if (maxResizeFactor > 1) return sourceImage;
            var width = maxResizeFactor * sourceSize.Width;
            var height = maxResizeFactor * sourceSize.Height;
            UIGraphics.BeginImageContext(new CGSize(width, height));
            sourceImage.Draw(new CGRect(0, 0, width, height));
            var resultImage = UIGraphics.GetImageFromCurrentImageContext();
            UIGraphics.EndImageContext();
            return resultImage;
        }
    
        // resize the image (without trying to maintain aspect ratio)
        public static UIImage ResizeImage(this UIImage sourceImage, float width, float height)
        {
            UIGraphics.BeginImageContext(new SizeF(width, height));
            sourceImage.Draw(new RectangleF(0, 0, width, height));
            var resultImage = UIGraphics.GetImageFromCurrentImageContext();
            UIGraphics.EndImageContext();
            return resultImage;
        }
    
        // crop the image, without resizing
        public static UIImage CropImage(this UIImage sourceImage, int crop_x, int crop_y, int width, int height)
        {
            var imgSize = sourceImage.Size;
            UIGraphics.BeginImageContext(new SizeF(width, height));
            var context = UIGraphics.GetCurrentContext();
            var clippedRect = new RectangleF(0, 0, width, height);
            context.ClipToRect(clippedRect);
            var drawRect = new CGRect(-crop_x, -crop_y, imgSize.Width, imgSize.Height);
            sourceImage.Draw(drawRect);
            var modifiedImage = UIGraphics.GetImageFromCurrentImageContext();
            UIGraphics.EndImageContext();
            return modifiedImage;
        }
    }
    
  • Lainio47Lainio47 USMember ✭✭

    @JalesC said:
    Update this code

    public static class ExtensionMethods
    {
    // resize the image to be contained within a maximum width and height, keeping aspect ratio
    public static UIImage MaxResizeImage(this UIImage sourceImage, float maxWidth, float maxHeight)
    {
    var sourceSize = sourceImage.Size;
    var maxResizeFactor = Math.Max(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height);
    if (maxResizeFactor > 1) return sourceImage;
    var width = maxResizeFactor * sourceSize.Width;
    var height = maxResizeFactor * sourceSize.Height;
    UIGraphics.BeginImageContext(new CGSize(width, height));
    sourceImage.Draw(new CGRect(0, 0, width, height));
    var resultImage = UIGraphics.GetImageFromCurrentImageContext();
    UIGraphics.EndImageContext();
    return resultImage;
    }

      // resize the image (without trying to maintain aspect ratio)
      public static UIImage ResizeImage(this UIImage sourceImage, float width, float height)
      {
          UIGraphics.BeginImageContext(new SizeF(width, height));
          sourceImage.Draw(new RectangleF(0, 0, width, height));
          var resultImage = UIGraphics.GetImageFromCurrentImageContext();
          UIGraphics.EndImageContext();
          return resultImage;
      }
    
      // crop the image, without resizing
      public static UIImage CropImage(this UIImage sourceImage, int crop_x, int crop_y, int width, int height)
      {
          var imgSize = sourceImage.Size;
          UIGraphics.BeginImageContext(new SizeF(width, height));
          var context = UIGraphics.GetCurrentContext();
          var clippedRect = new RectangleF(0, 0, width, height);
          context.ClipToRect(clippedRect);
          var drawRect = new CGRect(-crop_x, -crop_y, imgSize.Width, imgSize.Height);
          sourceImage.Draw(drawRect);
          var modifiedImage = UIGraphics.GetImageFromCurrentImageContext();
          UIGraphics.EndImageContext();
          return modifiedImage;
      }
    

    }

    Wow awesome code!!

  • NinineaNininea USUniversity ✭✭✭

    Hey,

    I need to save image thumbnail >> here Is simple code for c# , but it isn't valid for xamarin.mac :

    Image image = Image.FromFile(fileName);
        Image thumb = image.GetThumbnailImage(120, 120, ()=>false, IntPtr.Zero);
        thumb.Save(Path.ChangeExtension(fileName, "thumb"));
    

    Any solution?

Sign In or Register to comment.