Tip: “Path” Control in XAML – Boredom Challenge Day 21


The default controls in Visual Studio for WPF, Windows Phone and WinRT contain a XAML control called Path, which allows us to display simple shapes and drawings in our apps. The advantage of using it comes from the fact that it uses vectoral graphics, which provides us flexibility in several situations, the most prominent of which is, of course, resizing.

What it does is mix parts of different shapes together via a special markup syntax.

What it does is mixing parts of different shapes together via a special markup syntax.

For example, the above Skype logo is a Path object, which looks like the following in our XAML code:

        <Path Stretch="Uniform" UseLayoutRounding="False" Fill="White" Height="50" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="145,100,0,0" Data="F1 M 15.364,15.168C 14.9453,15.7707 14.3307,16.2427 13.5267,16.5813C 12.7213,16.9186 11.7707,17.088 10.6693,17.088C 9.35334,17.088 8.26267,16.86 7.4,16.3933C 6.79067,16.06 6.29467,15.612 5.91334,15.0546C 5.52801,14.4973 5.33601,13.9507 5.33601,13.4173C 5.33601,13.1027 5.45467,12.8306 5.68934,12.6066C 5.92533,12.3826 6.228,12.2693 6.58801,12.2693C 6.88267,12.2693 7.13334,12.36 7.33734,12.5333C 7.54134,12.708 7.71334,12.964 7.85334,13.3C 8.02001,13.688 8.20267,14.0133 8.39468,14.276C 8.58801,14.5293 8.86001,14.744 9.21201,14.9133C 9.55868,15.0813 10.02,15.164 10.5973,15.164C 11.388,15.164 12.028,14.9933 12.5173,14.656C 13.008,14.316 13.244,13.9013 13.244,13.4C 13.244,13.0014 13.116,12.684 12.856,12.436C 12.5933,12.1867 12.2533,11.996 11.8307,11.8627C 11.4067,11.728 10.8373,11.5853 10.124,11.436C 9.16801,11.2267 8.36667,10.9827 7.71734,10.704C 7.06667,10.4226 6.55067,10.04 6.16801,9.55463C 5.78268,9.06666 5.592,8.45866 5.592,7.74C 5.592,7.05335 5.79333,6.43732 6.2,5.904C 6.604,5.36933 7.18668,4.95866 7.952,4.672C 8.71334,4.38667 9.60934,4.24403 10.6333,4.24403C 11.4533,4.24403 12.164,4.33866 12.764,4.52799C 13.364,4.72001 13.8613,4.97065 14.2627,5.284C 14.6587,5.60001 14.9493,5.92933 15.1373,6.27869C 15.3227,6.628 15.4133,6.97065 15.4133,7.30134C 15.4133,7.61066 15.2933,7.89066 15.06,8.13734C 14.828,8.38264 14.528,8.508 14.1787,8.508C 13.864,8.508 13.6187,8.43464 13.4453,8.27998C 13.2787,8.13334 13.104,7.896 12.916,7.56666C 12.676,7.10401 12.3907,6.74799 12.0573,6.49198C 11.7307,6.24 11.1947,6.10932 10.452,6.11066C 9.76267,6.11066 9.21201,6.25065 8.79467,6.52665C 8.37467,6.80668 8.17467,7.13065 8.17467,7.51066C 8.17467,7.75065 8.24401,7.95069 8.38267,8.12397C 8.52001,8.29731 8.71734,8.44666 8.96667,8.57331C 9.21734,8.70267 9.47334,8.80399 9.728,8.87333C 9.98934,8.94666 10.4173,9.05335 11.0147,9.19465C 11.7653,9.35465 12.4453,9.53602 13.0573,9.73331C 13.6667,9.92933 14.1867,10.1693 14.616,10.4533C 15.0493,10.736 15.388,11.096 15.6293,11.5333C 15.872,11.9706 15.9947,12.5027 15.9947,13.1307C 15.9947,13.8867 15.7827,14.5667 15.364,15.168 Z M 20.4413,12.2106C 20.52,11.708 20.5613,11.1933 20.5613,10.6667C 20.5613,5.19999 16.132,0.769325 10.664,0.769325C 10.14,0.769325 9.624,0.812019 9.11868,0.890663C 8.20801,0.326668 7.13201,7.62939e-006 5.97867,7.62939e-006C 2.676,7.62939e-006 0,2.67731 0,5.98133C 0,7.13468 0.32534,8.20936 0.890678,9.12269C 0.810669,9.628 0.768005,10.14 0.768005,10.6667C 0.768005,16.1333 5.2,20.564 10.664,20.564C 11.1893,20.564 11.7053,20.524 12.2107,20.444C 13.1213,21.0067 14.1973,21.3333 15.3507,21.3333C 18.656,21.3333 21.3333,18.656 21.3333,15.3533C 21.3333,14.2 21.004,13.1253 20.4413,12.2106 Z "/>

The extremely long string value in the Data property of the Path object is how our shape is defined; meaning, it is the mathematical calculation. We can find many icons and symbols for Path control on the internet (I usually use http://www.thexamlproject.com, which contains hundreds of them). However, if you are interested in how you can create them yourself, Blend can import Adobe Illustrator files, so could draw them in Adobe Illustrator and import them to your project via Blend. And if you wonder how the syntax works, this article might be a good place to start.

Now, you might be saying “Why bother with this when I can just use images?”. Well, there are a few specific situations where using a Path has advantages.

The most important is, naturally, resizing.

We all know that if you resize an image to a bigger size than the original, it will look blurry; but if you resize a big image to a too small size (by resizing the Image control), it will still look bad (like in video games when you turn off anti-aliasing). In those cases, you would need to have the same image in two different sizes to be able to display them properly. Using a Path is also better if you want to use a “Zoom” function in your app, as images will look bad when zoomed in. Also, if you are going to support multiple resolutions (which is nearly always needed in WPF and WinRT since the resolution range is quite wide), and if there are any parts of your interface that resize according to the resolution, using images in those parts is not a good idea. For example, WinRT apps support minimum 1280×768, but it also supports 2560×1600, and your app may run on either.

How it normally looks.

How it normally looks.

How it looks if you zoom in (or resize it in a larger resolution).

How it looks if you zoom in (or resize it in a larger resolution).

Another advantage of using a Path is that it is more flexible. You can play with a Path’s properties; for example, you can change its colors, which you could use to show if something is enabled – disabled. And if you put some effort in it (or find a suitable Path), you can even create an icon or symbol consisting of multiple paths, and then animate them for really cool effects (such as multiple different shapes assembling and creating your logo).

The downside of vector graphics, however, is that they are harder to create. But, if you search on the internet, you’ll see that there are software for converting bitmap images to vectors (but they are not perfect, of course).

So, if you feel the need to support different resolutions or resizing without worrying about quality loss, or need to play with the properties your icons or symbols, you can use vector graphics with a Path control.

By the way, I really suggest you to check http://www.thexamlproject.com, even if you won’t be using vector graphics because you can download the logos there as .png files too. It is quite useful. 🙂

Thank you for reading.



Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s