Embedding and Referencing Fonts in your WPF application

I found this is the easiest way to it. First of all you have to add desired font to your project. I usually create some sort of “Resources” folder in such cases.

embedFont1

It is very important to set properties of your newly added recourse as follows.

embedFont2

Build action must be set to “Content” and it must be copied to application working folder. You can set Copy always or Copy if newer either one will work.

This is how you can reference your font from XAML code.

embedFont3

“.PathToResourceFolder/#FontName”

embedFont3

Basically font file will be copied to build output directory and will be loaded dynamically by .NET runtime.

Microsoft provided other ways to do this. It’s possible to embed font as resource into application or some other referenced assembly. However I found this most useful.

Some other solutions to same topic:

http://wpf.nickthuesen.com/?p=8

http://msdn.microsoft.com/en-us/library/ms753303.aspx

http://blog.tremaynechrist.co.uk/post/2009/12/22/Embedding-Fonts-In-WPF-Using-Visual-Studio.aspx

Get the MD5 hash of a file in .Net

MSDN has a page on how to get the MD5 hash of a string.

Easy enough, but if you follow their example exactly for a file and use File.ReadAllText() to get the string, you will get the wrong MD5 string for binary files. Instead, use File.ReadAllBytes() to bypass encoding issues. (This also applies to SHA1 hashing.)

   12  private static string GetMD5Hash(string filePath)

   13         {

   14             byte[] computedHash =

   15                 new MD5CryptoServiceProvider()

   16                 .ComputeHash(File.ReadAllBytes(filePath));

   17 

   18             var sBuilder = new StringBuilder();

   19             foreach (byte b in computedHash)

   20             {

   21                 sBuilder.Append(b.ToString("x2").ToLower());

   22             }

   23             return sBuilder.ToString();

   24         }

About me

Bizic Bojan is Freelance Software Development Consultant with focus on C++, Qt and .NET based software solutions development. 

 

Disclaimer:

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.