Mobile Line Of Business

Richard Jones (MVP)

  Home  |   Contact  |   Syndication    |   Login
  202 Posts | 0 Stories | 36 Comments | 0 Trackbacks

News

Welcome to the Mobile Line Of Business Blog

Tag Cloud


Archives

About

So today  I finally got around to updating my template libraries to cope with different screen DPI's (dot's per inch).

As you all know Windows Mobile has different DPI's based on the resolution of the screen.    I've been using 320x240 size devices for much of what I do,  however 640x480 (i.e VGA) is now becoming more and common.     To make forms look nice any bitmap that you drop onto them needs to look good at 96, 196 DPI to cope with this two resolutions.    Of course we other screen resolutions we have additional devices to cope with.

So what we need is some code that can decide based on DPI what images to use.  Working backwards lets show you the end result.   This is a VGA device with high 196 DPI graphics :

 

image

Click image to get the full effect.

 

Now the same application but on a lower DPI device -

image

Once again, click for details,

 

This is how it works.   In our project I have a set of embedded resources for all of the images, like this -

image

Note that each image in our project has a build action of Embedded Resource.

All we then need todo is a make a decision on which image to use based on the DPI of the device.    I came up with the following method to return available DPI for any given form -

 

public static int DPI(Form frm)
        {
            SizeF currentScreen = frm.CurrentAutoScaleDimensions;
            int dpi = (int)currentScreen.Width;
            return dpi;
        }

Now we have a way of getting the DPI of our application,  we can then load an appropriate image into any picturebox control etc.   This is achieved with the following  -

 

int dpi=DPI(this);

private Bitmap loadimage(string imageName)
        {

            string resource = "OURNAMESPACE.Images.";
            switch (dpi)
            {
                case 192:
                    resource += "192DPI";
                    break;
                default:
                    break;

            }
            return new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().
                GetManifestResourceStream(resource + imageName));
        }

 

That's about it.   To use just do something like this -

this.pbicon.Image = loadimage("Asterisk.png");

Note that you have to prefix any images other than the standard 96 DPI ones  with  '196DPI'

posted on Saturday, November 22, 2008 2:26 PM