First steps with .NET Compact Framework – not that bad at all!

by DotNetNerd 26. November 2010 18:58

Over the last couple of weeks, I have been working on new features for a PDA application which is build using the .NET Compact Framework. It’s the first time I have worked with the framework, and the first things I heard were not too uplifting. Jokes about it basically being a wrapper around NotImplementedException do make you wonder just how painful it will be.

I have to say though that it has actually been a pretty good experience so far! Maybe having the bar of expectations set just above groundlevel to start with makes it easier to make me happy – who knows?! I can conclude that debugging and deploying to the device has been smooth – just plug it in, press debug and you have all the usual debuggy goodness that Visual Studio provides.

The challange with .NET CF is that when making the framework smaller Microsoft really went in hard, so you will have to find alternate ways to get stuff done.

WebClient old friend, where are you?

One of the first things I needed to do outside the realm of getting data from a database and binding it, was to request an image and bind it to a PictureBox. WebClient is usually the way to go, but HEY, WHO MOVED MY CHEESE? “No WebClient, really?” was my first thought. Good thing making a webrequest can be done almost as easilly with the WebRequest class so this was what I came up with.

private delegate void ShowImageDelegate(Bitmap bitmap);
private string url = "";
private WebRequest request;

private void LoadImage()
    request = WebRequest.Create(url);
    request.BeginGetResponse(ImageDownloadDone, url);

private void ImageDownloadDone(IAsyncResult result)
    using(var response = request.EndGetResponse(result))
        using (var responseStream = response.GetResponseStream())
            var bitmap = new Bitmap(responseStream);

private void ShowImage(Bitmap bitmap)
    if (InvokeRequired) Invoke(new ShowImageDelegate(ShowImage), new[] { bitmap });
    else image.Image = bitmap;

To be honest another challange, for a guy who has hardly build a WinForms app for 6 years, was to remember how to handle the invocation stuff :)

Scaling a BitMap? Just pass it and a size to the constructor – no?

Next thing on the agenda was to scale the image so it would fit. This time it was not a class that was missing, but the constructor that makes it easy to scale the image. It was actually a while since I have had to use the Graphics class, but I think I came up with a pretty elegant solution.

public static Bitmap ScaleImage(Bitmap bitmap, int width, int height)
    double multiplier = height > width ?
        (width / (double)bitmap.Width) : (height / (double)bitmap.Height);
    Bitmap scaledImage = new Bitmap((int)(bitmap.Width * multiplier),
        (int)(bitmap.Height * multiplier));
    using (Graphics g = Graphics.FromImage(scaledImage))
        g.DrawImage(bitmap, new Rectangle(0, 0, scaledImage.Width, scaledImage.Height),
            new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
    return scaledImage;

Conclusion so far

So far it has turned out to be less troublesome to work with the .NET CF than I had expected. Actually it has been quite fun to be forced solve well known tasks a little more low-level than usual in the full blown .NET framework. That being said the fun probably mostly consists in knowing I won’t be doing this every day over long periods of time, because going the long way around doing stuff will probably get tedious over time.

Who am I?

My name is Christian Holm Diget, and I work as an independent consultant, in Denmark, where I write code, give advice on architecture and help with training. On the side I get to do a bit of speaking and help with miscellaneous community events.

Some of my primary focus areas are code quality, programming languages and using new technologies to provide value.

Microsoft Certified Professional Developer

Microsoft Most Valuable Professional

Month List

halk tv