Pixel-perfect Multi-DPI Images in WPF (Part 1)

View source on GitHub.

See also: Part 2 and Part 3.

I’ve written previously about DPI-awareness in the Windows Presentation Foundation and how to specify measurements in pixels rather than Device Independent Units (DIUs). Something else to consider is image scaling – unlike the Windows Ribbon control or WinRT, WPF has no in-built mechanism for displaying different images according to the system’s DPI setting. This is a nuisance.

To illustrate the problem, I created 5 images. Left-to-right, the image DPIs (vertical and horizontal) are: 72, 96, 120, 144 and 192. The image dimensions are: 32x32px, 32x32px, 40x40px, 48x48px and 64x64px. I purposely used single-pixel-wide lines to make any stretching obvious.

Original Images

Read on to see what WPF does with these images.

Continue reading “Pixel-perfect Multi-DPI Images in WPF (Part 1)”

P/Invoke and ChooseFont (Comdlg32.dll)

Mister Goodcat wrote a good article a little while ago about using P/Invoke in Silverlight 5 to display the native font chooser dialog with the ChooseFont function. I couldn’t get the code working for a 64-bit executable, however, as the definition of the CHOOSEFONT structure on MSDN he used as a basis for the managed implementation doesn’t match with what’s in the Windows SDK. I’ve created a page on PInvoke.net for the function that includes a definition of CHOOSEFONT that works in both 32-bit and 64-bit versions of Windows.

Screenshot of Font Chooser Dialog

Retrieving Windows 8 Theme Colours

See also: Windows 8 Theme Colours Reference.

Windows 8 doesn’t offer developers access to system theme colours, unlike its cousin, Windows Phone. Even for a version 1 product, this seems like a strange omission. Then again, we still don’t have a working public API for retrieving the Aero glass colour (or whatever we call it now that Aero’s gone) 6 years after the release of Windows Vista.

The functions that the system uses to retrieve colours are defined in UxTheme.dll. In particular, we’re interested in GetImmersiveColorSetCount (export #94), GetImmersiveColorFromColorSetEx (export #95), GetImmersiveColorTypeFromName (export #96), GetImmersiveUserColorSetPreference (export #98) and GetImmersiveColorNamedTypeByIndex (export #100). Relying on undocumented functions is a bad idea, and will cause your program to fail certification, so you won’t be able to use them in apps distributed through the Windows Store anyway.

For desktop developers who still want to use these functions, read on. Just assume that they’ll break in future versions of Windows (or even with patches to Windows 8).

Screenshot of Windows 8 Colours

Continue reading “Retrieving Windows 8 Theme Colours”

FastPictureViewer Codec Pack, Windows 8 and WPF don’t play nicely together

Update (2012-09-06): After some trial and error, it seems the Canon Hack Development Kit (CHDK) component is causing the issue. If you’re experiencing the same problem, try removing this component.

Update (2012-09-05): The FastPictureViewer support team was unable to reproduce the issue, but suggested it might have something to do with the JPEG Auto-Rotate feature. I was able to resolve the issue by installing only the following components of the codec pack: Photoshop PSD, Lightroom Previews, Adobe DNG, Canon CR2, CRW, PDF Thumbnails and Control Panel. This suggests one of the other components was causing the conflict on my machines.

I use the FastPictureViewer Codec Pack for viewing DNG images in Windows Explorer, amongst other things. Unfortunately, however, installing version 3.3.0.69 of the codec pack in Windows 8 (64-bit) caused my installations of Visual Studio 2010, Visual Studio 2012, Blend 4 and Blend for Visual Studio 2012 to stop working to various extents. Both versions of Blend crash silently. The start pages in both versions of Visual Studio fail to load, with VS2010 displaying a ‘Content Load Error’ relating to ‘System.Windows.Baml2006.TypeConverterMarkupExtension’ throwing an exception. The error message displayed where the Visual Studio 2012 Solution Explorer belongs was more useful: ‘System.Runtime.InteropServices.COMException: The codec is in the wrong state. (Exception from HRESULT: 0x88982F04)’.

Uninstalling the codec pack caused everything to start working again. I’ll update this post if/when a future release resolves the incompatibility.

Screenshot of Visual Studio 2010 Error

Screenshot of Visual Studio 2012 Error 

Office 2013: Further Evidence of the Demise of ClearType?

Poking around the Office 2013 Customer Preview, one thing that caught my eye immediately was the lack of ClearType font smoothing. Nearly all user interface elements use ‘greyscale’ font smoothing (as opposed to ClearType’s sub-pixel smoothing). I’ve highlighted the parts of Word 2013 that still use ClearType:

Screenshot of ClearType in Microsoft Word 2013Screenshot of ClearType in Microsoft Word 2013

The text in cells in Excel 2013 still respects the system’s ClearType setting, and Outlook 2013 is a real mess (about half-and-half). OneNote 2013 seems to have retained ClearType in most places (outside the ribbon control).

Long Zheng earlier noted the absence of ClearType from the Metro environment and there are several possible reasons for it falling out of favour at Microsoft. Sub-pixel anti-aliasing isn’t necessarily suited to tablets that need to support multiple orientations (unless different sub-pixel orderings are taken into account), and screens with high pixel densities can get away with greyscale font smoothing.

I fear that this is another case of the tablet tail wagging the Microsoft dog – desktop and laptop users of Office 2013 (and Metro-style apps in Windows 8) will have to deal with lower quality text due to the new hardware Microsoft is targeting with its upcoming releases.

The Office 2013 Customer Preview is, of course, just that, and things might change before RTM. Regrettably this wasn’t fixed for the Office 2013 RTM.

Keiki Usage Meter 3.1.0 Released

I’ve released an update for Keiki Usage Meter. There has been a lot of behind-the-scenes work, but the user experience hasn’t changed drastically. Visit the website to find out what’s new.

I’m using this program as something of a testbed for some engineering work that I’ll talk about in future posts, including supporting pixel-perfect bitmap images at multiple DPI settings in WPF and using the Visual Styles APIs to draw native-looking controls (with animation). I’ve also taken the opportunity to learn a bit about localising programs, and v3.1.0 contains three user interface languages: English (Australia), English (United States) and Japanese (Japan) – thanks to Miho Inaba (稲葉美穂) for help with the latter.

WPF: Using System Colours in Animations

WPF provides access to the Windows system colours through the System.Windows.SystemColors class. As the MSDN documentation indicates, it’s possible to bind to these colours dynamically using the *Key resources, so when the system colours change, so will the appearance of your application.

This code will set the colour of a TextBlock to the ‘GrayText’ system colour, and will change automatically if the user switches themes:

In many cases, there’s no reason not to bind dynamically. Problems arise with animations, however. From MSDN:

You can’t use dynamic resource references or data binding expressions to set Storyboard or animation property values. That’s because everything inside a ControlTemplate must be thread-safe, and the timing system must Freeze Storyboard objects to make them thread-safe. A Storyboard cannot be frozen if it or its child timelines contain dynamic resource references or data binding expressions.

This means that you’re stuck using static references to system colours in your animation storyboards – if the user changes the system colours while your application is running, your storyboards won’t update automatically. Worst-case scenario, you might end up with unreadable text.

I don’t think there are any workarounds that don’t involve reloading windows and/or resource dictionaries (depending on where the style code is located). Reloading a window is easy enough – just be careful if you’re closing your application’s last window: the program will shut down if there are no remaining references to any windows.

The method for reloading a resource dictionary is a bit less obvious. It seems that XAML files accessed by their pack URIs are already compiled, so the system colours will be fixed in place once the program starts up. To get around this, change the build action for the XAML file containing the resource dictionary to ‘Embedded Resource’. You can then (re)build the resource dictionary as you please using XamlReader:

Remove the old resource dictionary from your application’s merged resource dictionaries, then add the new one.

Creating a new resource dictionary like this seems to be fairly slow, so try to avoid doing it on a regular basis. To tell when the Windows theme or system colours have changed, you’ll need to listen for the window messages WM_THEMECHANGED and WM_SYSCOLORCHANGE. See this post for information about listening for window messages in WPF.

More on Segoe UI in Windows 8

Note: the following is based on the Windows 8 Release Preview (build 8400) and may not accurately reflect the final version of Windows 8. I’ll update this post if there are changes in future builds.

Updated: The fonts have been updated in the public beta (Windows Consumer Preview) and release candidate (Windows Release Preview) and I’ve changed the tables below to reflect this. I was also made aware of some additional information about changes to Segoe UI, including new stylistic sets and support for ligatures: you can read more at Subacme.

Further to my earlier post about Segoe UI Light in Windows 8, I’ve done some more research into the changes to the Segoe family of fonts. The Fonts class in the .NET Framework was helpful for retrieving information about the fonts.

First off, Segoe Print and Segoe Script and their bold variants have not been modified and are still labelled version 5.02 and 5.00, respectively. All existing variants of Segoe UI have been updated, and there is one new variant: Segoe UI Semilight. The latter is in fact used as the caption and small caption font in Windows 8 – Windows Vista and Windows 7 used Segoe UI (Regular). The Windows Phone 7 platform includes Segoe WP Semilight, and Segoe UI Semilight seems to be a superset of that font.

Apparently new in the Windows 8 Release Preview are three italic variants: Segoe UI Light Italic, Segoe UI Semibold Italic and Segoe UI Semilight Italic.

The following tables summarise the differences in glyph and character counts between the Windows 7 and Windows 8 versions of the Segoe UI font family:

Segoe UI Regular

  Windows 7 Windows 8 Developer Preview Windows 8 Consumer Preview Windows 8 Release Preview
Version 5.01 5.12 5.16 5.24
Glyphs 2899 4281 4490 4516
Characters 2658 3283 3423 3404

Segoe UI Bold

  Windows 7 Windows 8 Developer Preview Windows 8 Consumer Preview Windows 8 Release Preview
Version 5.01 5.12 5.15 5.24
Glyphs 2899 4290 4428 4442
Characters 2658 3283 3421 3401

Segoe UI Italic

  Windows 7 Windows 8 Developer Preview Windows 8 Consumer Preview Windows 8 Release Preview
Version 5.01 5.10 5.15 5.22
Glyphs 2381 2478 2597 3077
Characters 2130 2180 2299 2525

Segoe UI Bold Italic

  Windows 7 Windows 8 Developer Preview Windows 8 Consumer Preview Windows 8 Release Preview
Version 5.01 5.10 5.15 5.22
Glyphs 2381 2478 2597 3077
Characters 2130 2180 2299 2525

Segoe UI Light

  Windows 7 Windows 8 Developer Preview Windows 8 Consumer Preview Windows 8 Release Preview
Version 5.00 5.12 5.15 5.25
Glyphs 2380 4036 4178 4236
Characters 2131 3090 3230 3211

Segoe UI Light Italic

  Windows 8 Release Preview
Version 5.22
Glyphs 3089
Characters 2525

Segoe UI Semibold

  Windows 7 Windows 8 Developer Preview Windows 8 Consumer Preview Windows 8 Release Preview
Version 5.00 5.12 5.16 5.24
Glyphs 2380 4089 4228 4242
Characters 2131 3144 3231 3211

Segoe UI Semibold Italic

  Windows 8 Release Preview
Version 5.22
Glyphs 3089
Characters 2525

Segoe UI Semilight

  Windows 7 Windows 8 Developer Preview Windows 8 Consumer Preview Windows 8 Release Preview
Version N/A 5.12 5.15 5.24
Glyphs N/A 4034 4175 4189
Characters N/A 3090 3230 3210

Segoe UI Semilight Italic

  Windows 8 Release Preview
Version 5.23
Glyphs 3089
Characters 2525

Segoe UI Symbol

  Windows 7 Windows 8 Developer Preview Windows 8 Consumer Preview Windows 8 Release Preview
Version 5.00 5.33 5.46 5.55
Glyphs 3090 5122 6785 6846
Characters 2864 4547 5092 5217

Supported Unicode Blocks

For further information about which Unicode blocks are supported by the Segoe family of fonts, you can download my extended analysis here:

segoe-analysis.7z
2,825 bytes; SHA-1: DFFA40934DB3305054C40AF3FD6E7224F5CF1EFB