Welcome to MSDN Blogs Sign in | Join | Help

MelSam's Blog

Mel Sampat on Windows Mobile
Spot the Bug - Episode 2 solution and explanation

Thanks to everyone that participated in the second Spot the Bug contest this week. As "patters" pointed out, the application compatibility issue was hard coding icon sizes. Everyone else had very valid points about memory allocation, color capabilities etc., but remember - the focus is on maximizing compatibility for Windows Mobile.

Take a look at the following snippet from the contest code sample:

void CTestAppDlg::initializeListCtrl()
{
    // Create the imagelist and assign it to the listview control
    m_imageList.Create(32, 32, ILC_COLOR|ILC_MASK, 1, 1);
    m_wndListCtrl.SetImageList(&m_imageList, LVSIL_NORMAL);

    // Add an icon to the imagelist
    HICON hIcon = 
        (HICON)LoadImage(AfxGetResourceHandle(),
        MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 32, 32, 0);

    m_imageList.Add(hIcon);    
}

When creating the imagelist above, we're hard coding the width and height (32x32). When loading the icon, we're hard coding the same sizes again. As Windows Mobile developers, you should know that Pocket PC and Smartphone devices do not always use standard desktop icon sizes. Because these devices come in various DPIs and resolutions, the icon sizes vary.

Here's a handy reference table that will help you determine which icon sizes to include in your application.

Device DPI Small Icons Large Icons
Pocket PC (normal DPI) 96 16x16 32x32
Pocket PC (high DPI) 192 32x32 64x64
Smartphone (normal DPI) 96 16x16 32x32
Smartphone (high DPI) 131 22x22 44x44

To determine the correct icon size for the device your application is running on, use the GetSystemMetrics API. For example, GetSystemMetrics(SM_CXICON) will give you the correct "large" icon size, which is usually 32 pixels.

The GetSystemMetrics API is part of the Win32 SDK, so it's a good idea to use it for your desktop applications as well. This will be particularly important for Windows Vista compatibility because controls such as listviews may expect icons of different sizes.

Merely using this API is not enough though. You also have to include the actual icon resources of the expected sizes. Admittedly, this means more work for your art department and larger resource files or .exes. True, but if you don't have the right sized icons, the system will have to scale the image up or down, and thus make your icons look blurry.

Take a look at the following screenshots.

Screenshot 1

Above is a high DPI Pocket PC device where a 64x64 icon was expected, but 32x32 icon was available. The system scaled up the icon, thus making it blurry.

Screenshot 2

Above is a high DPI Smartphone device in which a 44x44 icon was expected, but 64x64 was available. In this case the system scaled down the available icon, again making it blurry. Due to jpg compression, these screenshots do not convey the exact amount of "blurriness", but users will notice a significant difference on real devices.

Bottom-line, don't hard code icon sizes in your code. Instead, use the API to query icon sizes from the system, and include appropriately sized icons in your application.

Feel free to discuss this issue or post any other thoughts about the contest so far. And get ready for the next round of Spot the Bug!

Posted: Wednesday, June 07, 2006 11:27 PM by MelSam
Filed under:

Comments

Kevin Cawley said:

Great info Mel... I am seeing this on my WM5 Smartphone where the application icon looks jagged.  We have the correct sizes, but the problem is the icon creator our designer is using doesn't support 131 DPI... got any suggestions or tools you use?

thanks
-kevin
# June 8, 2006 4:13 PM

MelSam said:

Kevin:
I'm not a graphics expert, but to the best of my knowledge, icons themselves don't have to worry about DPI. Icons only have to be the right size and color depth. I've used an icon editor called Microangelo (www.microangelo.us) with great success on a few projects in the past. Within Microangelo, I'd create icons that are the appropriate size (see table above), and "True Color" - which is what Windows Mobile supports. I think "True Color" means 16M colors, but just to be safe, I include a 256 color icon of the same size as well. Hope this helps.

-Mel
# June 8, 2006 5:24 PM

glacier said:

前段时间我们介绍了微软大牛MelSam的“spot the bug”专栏,本周他针对不同分辨率下icon图标的尺寸问题作了整理。开发者需要注意的是,在high DPI 的情况下,图标并不是标准的大小。
# June 12, 2006 12:01 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker