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.

Android:


// 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();
matrix.PostRotate(-90);      
	       
// 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;

iOS:

	 

// assume something hands us a bitmap
UIImage myImg = GetImageFromWebService();
       
SizeF newSize = new SizeF(myImg.Size.Height, myImg.Size.Width);
	
UIGraphics.BeginImageContext(newSize);
CGContext ctx = UIGraphics.GetCurrentContext();
			
ctx.TranslateCTM(myImg.Size.Height, myImg.Size.Width); 
			
// scale
ctx.ScaleCTM(1f, -1f);
			
// rotate anti-clockwise
ctx.RotateCTM(1.57079633f);
			
// 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();
	
UIGraphics.EndImageContext();
		
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.

Advertisements
Rotating a Bitmap in Mono for Android and MonoTouch

2 thoughts on “Rotating a Bitmap in Mono for Android and MonoTouch

  1. Chris says:

    Have you tried using this code on a UIImage whose Height is not equal to it’s Width? I tried and got some weird results: the image somehow was rotated 180 degrees instead of 90.

    1. Yes, I had to do this for both Android and iOS. The iOS version is being tested at work right now before submitting to Apple. It pulls an image from a web service which is always wider than it is tall so I use the above code to rotate it to fit the iPhone’s screen – works fine. Can’t think what might be causing your issues.

Comments are closed.