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
Here you go, methods for resizing and cropping
You might also like to try ProtoPad, it allows you to very quickly experiment with stuff like this.
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
I believe Math.Min should have been used, not
Math.Max
. If you want both the width and the height to not exceed the max values, you must choose the least of the two to use as the resize factor.var maxResizeFactor = Math.Min(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height);
Otherwise, great work. Much appreciated.
Thanks Jason, I've already figured it out myself, and forgot the post a comment, "min" makes much more sense!
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
Thanks for your methods for resizing and cropping.
For Retina Display contexts the images produced with your methods loses resolution, you must use:
UIGraphics.BeginImageContextWithOptions(new SizeF(width, height), false, 2.0f);
instead of:
UIGraphics.BeginImageContext(new SizeF(width, height));
Great work.
Thanks SO MUCH for the methods! Extremely helpful!
there's also http://forums.xamarin.com/discussion/14269/objective-c-image-crop-function for a shorter crop function.
@ChrisHonselaar Thanks for the functions - quite helpful.
Can you also point to Android and WP specific implementation of these resizing and cropping methods?
Update this code
Wow awesome code!!
Hey,
I need to save image thumbnail >> here Is simple code for c# , but it isn't valid for xamarin.mac :
Any solution?