Rotating a Bitmap in Mono for Android and MonoTouch

In a post last year, I wrote about a need I had to display a bitmap received from a web service but rotated by 90 degrees. This was for an Android application I was writing at the time using Eclipse and Java. As I recently stated, I’ve been looking at using C# to build iOS apps and decided to go the whole hog and checkout Mono for Android too. So I began by porting that application to Mono for Android which took around a week. Now I’ve nearly finished porting it to MonoTouch, the iOS equivalent. I can’t express how much I love the ability to use C# to write for these two platforms. Everyone associated with all things Mono should be congratulated for a terrific achievement, especially when you consider that even Microsoft are using Mono to write iOS apps now! Who’d have thunk it? :)

Anyway, for completeness I thought I would show the rotating bitmap code again but this time using C# code for the Android and iOS flavours of Mono. Hope it’s of use to someone out there.


// assume something hands us a bitmap
Bitmap myBitmap = GetImageFromWebService();

// get the width and height of the current view
Display d = WindowManager.DefaultDisplay;
int x = d.Width;
int y = d.Height;
// scale it to fit the screen
Bitmap scaledBmp = Bitmap.CreateScaledBitmap(myBitmap, y, x, true);
// create a matrix, rotate it anti-clockwise by 90 degrees
Matrix matrix = new Matrix();
// create a new rotated bitmap using the our original bitmap and the matrix measurements
Bitmap rotatedBmp = Bitmap.CreateBitmap(scaledBmp, 0, 0, scaledBmp.Width, scaledBmp.Height, matrix, true);    
return rotatedBmp;



// assume something hands us a bitmap
UIImage myImg = GetImageFromWebService();
SizeF newSize = new SizeF(myImg.Size.Height, myImg.Size.Width);
CGContext ctx = UIGraphics.GetCurrentContext();
ctx.TranslateCTM(myImg.Size.Height, myImg.Size.Width); 
// scale
ctx.ScaleCTM(1f, -1f);
// rotate anti-clockwise
// draw a new image with the same size
ctx.DrawImage(new RectangleF(0, 0, myImg.Size.Width, myImg.Size.Height), myImg.CGImage);
UIImage rotatedImage = UIGraphics.GetImageFromCurrentImageContext();
return rotatedImage;

To be fair, the Android version is not that different from before (as you’d expect really) what with Java and C# being of similar syntax. The iOS version however is quite a bit different and perhaps a little bit more involved but it’s still fairly easy. You can experiment with the values passed to the various CTM (current transformation matrix) methods or change the size of the newly drawn image in the DrawImage method to see how they affect the resulting image display.

As for why I rewrote the Java based app in Mono C# well, for one C# is IMHO a nicer language than Java, and two, I extracted the business logic out into a separate assembly which is now shared between both the Android and iOS applications which means I only have to add new features or fix bugs in one place. It’s also been a lot easier and quicker to create the iOS version of the app than I’m sure it would have been if I had gone down the Objective-C route.

Rotating a Bitmap in Mono for Android and MonoTouch

2011 Review

Just had to get a quick end of year post up. Yep, it’s new years eve, I’ve just finished watching Rise of The Planet Of The Apes (not bad), had too much beer (not good), and thought it would be a good idea to ramble on here for a while. At the very least it’s a good excuse to use my shiny new quad-core iMac so here goes.

2011 for me wasn’t too bad. I started a blog! I even started a new job in March, got stuck into NServiceBus for a good while and really enjoyed it. It’s a great framework for reliable, durable messaging and I thoroughly recommend it. On the subject of messaging, one really interesting library that I’ve spent a little bit of time looking at is ZeroMQ, a socket library that has the familiar messaging patterns at it’s heart even though it’s not a servicebus. Wow, that thing is fast! I’ll definitely be evaluating it in more depth over the next few months.

Toward the latter part of the year I got into Android development using Eclipse and Java. I wrote an order tracking application in my spare time and then unveiled it at work where it was received as I hoped it would be. This led to further ideas about where we could go with mobile dev. The obvious conclusion being that you can’t do mobile only for one platform so some time was spent evaluating the best way to target the iPhone as well as Android. In then end it looks like the fantastic work that’s gone into the Mono framework and MonoTouch and Mono for Android in particular means that we can make the best use of our existing in-house C# skills to get mobile apps out there way quicker than we ever could have done if we’d had to learn each platform from scratch. That’s not to say the time writing the Java version was wasted, it wasn’t. In fact, knowing key Android Concepts such as Activities and Intents are still crucial when using Mono. This knowledge helped me port the Java version to Mono in record time – about a week – and I’m already about 50% of the way into a MonoTouch iPhone version.

My current favourite Git hosting provider ( also released a free Git Gui tool for Mac OS X in the Mac App Store called SourceTree. I have to say, that despite being used to the command line when working with Git, SourceTree is so damn good I just have to use it, and best of all, it’s free so that’s another recommendation.

Goals for next year include really attacking Android and iPhone development. Mobile is definitely where it’s at/going to be, and I think Mono is the best tool to take me there in the shortest time. I may even get into targeting the Mac App store with it too. I wouldn’t mind learning a little bit of Ruby just for the hell of it. I’m trying to get my 13 year old daughter interested in programming and thought Ruby might be a good place to start, especially with KidsRuby. Looks like a really cool way to get children involved.

Okay, where I am, it’s 11:30-ish. Half an hour to go until I have to start putting those plans in place. Time to get another drink.

Happy New Year.

2011 Review