Mobile Scanning Application Framework

Back in April at the Toronto Code Camp 2009 after my Windows Mobile Developer Boot-Camp presentation I met Dan Douglas and we discussed about some Windows Mobile projects he has worked in his organization.  He recently blogged about a Mobile Scanning Framework he implemented and demonstrates one possible way to encapsulate bar code scanning input and validation of the data within a Windows Mobile application using .NET Compact Framework

Convert an Image to Grayscale with Compact Framework on Windows Mobile

I’m working with a customer and they have a requirement to convert an image to gray scale.  Using direct pixel access method discussed here and extension methods it’s pretty straight forward.

public static Bitmap ModifyColors(this Bitmap image, double redChange, double greenChange, double blueChange)
{
if (image == null)
return null;
Bitmap newBitmap = (Bitmap)image.Clone();

int width = newBitmap.Width;
int height = newBitmap.Height;

unsafe
{
BitmapData bd = newBitmap.LockBits(new Rectangle(0, 0, newBitmap.Width, newBitmap.Height),
ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
int sourceWidth = newBitmap.Width * System.Runtime.InteropServices.Marshal.SizeOf(typeof(PixelData));
if (sourceWidth % 4 != 0)
sourceWidth += (4 – (sourceWidth % 4));
Byte* bitmapBaseByte;
bitmapBaseByte = (Byte*)bd.Scan0.ToPointer();
PixelData* pPixel;
int redVal;
int greenVal;
int blueVal;
for (int y = 0; y < height; y++)
{
pPixel = (PixelData*)(bitmapBaseByte + y * sourceWidth);
for (int x = 0; x < width; x++)
{
redVal = (int)(pPixel->red * redChange) + (int)(pPixel->green * greenChange) + (int)(pPixel->blue * blueChange);
if (redVal < 0) redVal = 0;
if (redVal > 255) redVal = 255;
pPixel->red = (byte)redVal;

greenVal = (int)(pPixel->red * redChange) + (int)(pPixel->green * greenChange) + (int)(pPixel->blue * blueChange);
if (greenVal < 0) greenVal = 0;
if (greenVal > 255) greenVal = 255;
pPixel->green = (byte)greenVal;

blueVal = (int)(pPixel->red * redChange) + (int)(pPixel->green * greenChange) + (int)(pPixel->blue * blueChange);
if (blueVal < 0) blueVal = 0;
if (blueVal > 255) blueVal = 255;
pPixel->blue = (byte)blueVal;

pPixel++;

}
}

newBitmap.UnlockBits(bd);
}

return newBitmap;
}


Basically all we are doing is getting direct access to the pixel data using a pointer and modifying the RGB values with the parameters sent by the user.  In our main code where we want to convert the image, lets say we have an image in a PictureBox that we want to convert and store the result in a new PictureBox all we have to do is call one line of code:

pictureBox2.Image = pictureBox1.Image.ToGrayScale();

Here is a sample of the input/output:


image


And you can download the source for converting an image to grayscale here

Developing for Windows Mobile – Leveraging Existing .NET Code

I’ve been getting a few emails/questions on ‘Leveraging Existing .NET Code’ section of my Platform In Your Pocket session for TechDays so just wanted to share what I’ve been sharing with those emails.

First all credits go to Daniel Moth for his Sharing Assets between Windows Mobile and Windows Desktop he did back in 2006.  Daniel now works for the Parallel Computing Platform so be sure to check out his blog for tips and check out his PDC session here.

I only spent about 15mins on leveraging existing code and is only a small fraction of what Daniel covered in his session.  I always reference his blog posts when responding to emails so I’ll do the same here. His most recent post is on Sharing Assets Between the .NET Compact Framework and the .NET Framework has all relevant links.  There is an article in MSDN Magazine article which also goes through the concepts.

If you are a .NET developer looking at writing a Windows Mobile application make sure you leverage your existing code so you can get your product to your customers faster.

Good Luck and if you have more questions feel free to contact me!

Optimizing your Windows Mobile Applications

I’ve been consulting for Windows Mobile projects for quite a few years now and know most of the techniques to optimize custom Windows Mobile software.  During my ‘Platform in Your Pocket’ session for TechDays Canada, I went through some optimizations tips and tricks such as using StringBuilder as opposed to string or List<T> opposed to ArrayList things that you take for granted when developing on the desktop using the .NET Framework.  On Windows Mobile it’s more important because you are ‘memory constrained’ and can’t just ‘throw more RAM or CPU’ in a device to make your application perform better. 

During the session I didn’t have time to cover everything to optimize your code and a common question was ‘what else can I do to optimize my code’.  Here is a page on MSDN that lists a few ways to improve the performance of you Windows Mobile app using .NET Compact Framework which should help when you are developing your software.

Memory Management on Windows Mobile using .NET Compact Framework

During my Windows Mobile developer session in Calgary for TechDays Canada surprisingly a few people wanted to know more about the memory architecture of a .NET Compact Framework application.  I didn’t cover anything on that but the best reference for this if you want to learn more about it is the follow:

  1. MSDN Webcast: Microsoft .NET Compact Framework Memory Management by Chris Tacke (2006*)
  2. .NET Compact Framework Advanced Memory Management by Mike Zintel  (2004*)

These things go into a lot of the internals of the .NET Compact Framework CLR and the Windows CE memory architecture (Windows Mobile is based on Windows CE) but in my opinion are required readings if you are building Windows Mobile applications.

*Don’t worry about the years they where done as it’s still relevant when developing on Windows Mobile today.

NumericUpDown Control and Selecting Text (Revisited)

Back in Jan ’06 I wrote an post on selecting the text of a NumericUpDown control.  Peter Morris pinged me the other day to let me know that he has updated the code provided and added support for Validation events.  It’s great to see code being used by others and even greater to see when people like Peter share their changes back with the community.  Check out his updates here.

If you have used any code I’ve released via my blog, our Open Source products or any code from OpenNETCF partners either in production or just for a hobby project let me know and I can share your story!

Using Extension Methods in .NET CF 2.0

Extension methods are a great addition to the C# 3.0 Language Specification as it allows you to extend a class by adding new methods and extend the class with features you desire.  For the .NET Compact Framework developer this is available in Visual Studio 2008 and .NET Compact Framework 3.5

At OpenNETCF we have a few developer products but are still using .NETCF 2.0.  Even a lot of our consulting jobs still use .NETCF 2.0 (and sometimes 1.0) but more customers are now wanting CF3.5.

Getting used to extension methods and then not being able to use them in pre .NETCF3.5 projects is not fun.  After doing a little search I found this were Daniel explains using extension methods in .NET 2.0 and how the compiler “figures things out”. Basically the same thing applies for .NETCF 2.0.  You do require Visual Studio 2008 for this to work.

Basically what you have to do is add the following to your project:

namespace System.Runtime.CompilerServices
{
    public class ExtensionAttribute : Attribute
    {
    }
}

With that you can share extension methods you have written for .NETCF3.5 projects with .NETCF2.0 projects.

For example, I do a lot of custom drawing for controls on Windows Mobile and draw images with transparency.  Pre extension methods I would use something like this in .NETCF 2.0:

protected override void OnPaint(PaintEventArgs e)
{
    Bitmap image = new Bitmap(m_imagePath);
    ImageAttributes imageAtt = new ImageAttributes();
    Color transparentColor = image.GetPixel(0, 0);
    imageAtt.SetColorKey(transparentColor, transparentColor);
    e.Graphics.DrawImage(image, 
        new Rectangle(0,0,image.Width,image.Height), 
        0, 0, 
        image.Width, image.Height, 
        GraphicsUnit.Pixel, imageAtt);
    image.Dispose();
}

With the Attribute workaround above we create an extension method as follows:

public static void DrawImageTransparent(this Graphics graphics, 
    Bitmap image, 
    Rectangle destinationRect)
{
    ImageAttributes imageAtt = new ImageAttributes();
    Color transparentColor = image.GetPixel(0, 0);
    imageAtt.SetColorKey(transparentColor, transparentColor);
    graphics.DrawImage(image, 
        destinationRect, 0, 0, 
        image.Width, image.Height, 
        GraphicsUnit.Pixel, imageAtt);
}

And we can call it from our OnPaint method as follows:

using (Bitmap image = new Bitmap(m_imagePath))
    e.Graphics.DrawImageTransparent(image, new Rectangle(0, 0, image.Width, image.Height));

In the end we get a lot cleaner code and the extension method is portable to .NETCF3.5 and 2.0.

Exchange, WCF, Windows Mobile and .NET CF

From my recent DevTeach and EnergizeIT sessions I got a lot of interest in WCF Store and Forward feature in .NET CF 3.5 and I promised to put together a white paper on the topic. 

I just finished an article on Exchanging Data using Windows Mobile, Windows Communication Foundation, .NET Compact Framework and Exchange 2007 and posted it on our Community site.  It basically goes through some of the technical details with getting WCF and Exchange working using two sample applications, a Dispatch Sample and a Photo Sharing Sample.

Windows Mobile 6.1 Welcome Center and Compact Framework

For those not familiar, Windows Mobile 6.1 comes with a new application called Welcome Center which basically is a help system for users. 

WelcomeCenter

I was running some hopper tests on Task Manager 2.0 and looking through the hopper.log file I noticed the following line:

STATS: \Windows\welcomecenter.exe -> #NETCF_AGL_PARK_\Windows\welcomecenter.exe: Time in State =          0 secs ( 0.0%); Actions =      2; Visits =      2; Last visit =    196 actions ago

If you noticed #NETCF_AGL_PARK_ then you know that this means the application WelcomeCenter.exe uses Compact Framework and this is a welcoming surprise.  Let’s hope this trend continues with other apps from the Windows Mobile team and other teams within Microsoft!

As for Task Manager 2.0 here is a screen shot of the new version.

image

And yes we do know that Windows Mobile 6.1 has a Task Manager included but ours does not filter out any processes or applications, allows you to terminate processes, end applications, terminate threads, change thread priorities and view all threads and modules loaded for a process.  The same binary also works on different platforms such as Windows CE, Pocket PC 2003, Windows Mobile 5 and Windows Mobile 6. It’s also written using Compact Framework  and leverages the Smart Device Framework.