Tuesday, December 16, 2008

Looking through Images - working with transparent images in the .Net Compact Framework

In the .Net Framework it's quiet easy to work with transparent images. But if you want to work with transparent images on the .Net Compact Framework it's not so easy. The reason for this behavior is, that System.Drawing.Bitmap from the .Net CF don't support information from the Alpha-channel. You can read Png-files, but these information will be ignored.

Some, who know the know more of the operating systems for Smart Devices, Windows Ce and Windows Mobile, might say, that you could you use the AlphaBlending-Api. Sure, that is right. But you should know, that the AlphaBlending-Api is just a part required of Windows Mobile and not of Windows Ce. So you could use it only, if you know that Smart Device where you application should run, supports it.
But, are you really sure?

When I had to work with transparent images on Smart Devices, I couldn't be sure. To many different devices with to many different configurations of the operating system.

The problem was very easy. I had to combine two bitmaps like these...




When you using System.Drawing.Bitmap and set a color from the Bitmap as the transparent color with the ImageAttributes, the results are really...are really...I don't know...They are not really nice. It looks like that...

Transparent Images with the .Net Compact Framework by it's own

So, what you could you do, that this looks better, maybe like on the desktop?

It's a little bit difficult and you need a lot of steps, but it's possible.

At first, you have to convert the bitmap data. You need direct access to every single pixel of the bitmap. We must store all four bytes of a pixel in a separate array or file. That is only in the full .Net Framework possible. I used an array of this structure...


...which I stored in a file. That file, I had to copy in the file system of the Smart Device.

On the Smart Device, you have to read the file from the file system into your application. There, you have to combine your bitmap data with a 'normal' bitmap for showing it in the display.

For the combining of both bitmaps, I used that algorithm.


In my opinion, the result is much better, like on the full .Net Framework.

Pro's and Con's

OK, it looks smart and it could be very flexible with little bit more code. But you should know that this result is not for free.

I think the most important contra is, that this solutions is not really fast. It takes a few milliseconds to read the file from the file system, it takes a few more milliseconds to combine both bitmaps and a last it takes a few milliseconds for showing it on the display. OK, if you have smaller overlaying bitmaps, it isn't so much slower, but you will notice it quickly.

The second, not less important contra is, that you need more memory. If you use it with operating systems with are based on Windows Ce 5.0 or older, you have only a process size of 32 MBytes.



I told you a little about the concept how it is possible to work with transparent bitmap on the .Net Compact Framework. During my work with it, I created a whole library around it to solve some of the contras. It's still in using since nearly two month on different Smart Devices without any larger problems.

So be sure, it's possible to work with smart blended bitmaps on the .Net Compact Framework.

The complete screen in 320*240