Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
90 views
in Technique[技术] by (71.8m points)

c# - How to include external font in WPF application without installing it

How to include external font in WPF application without installing it

I tried this code

  System.Drawing.Text.PrivateFontCollection privateFonts = new    System.Drawing.Text.PrivateFontCollection();
  privateFonts.AddFontFile("C:\Documents and Settings\somefont.ttf");
  System.Drawing.Font font = new Font(privateFonts.Families[0], 12);
  this.label1.Font = font;

It working correctly in Windows Form Application but not in WPF.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This are two ways of doing this. One way is to package the fonts inside the application. The other way is to have the fonts in an folder. The difference is mostly the URI you need to load the files.

Package with Application

  1. Add a /Fonts folder to your solution.

  2. Add the True Type Fonts (*.ttf) files to that folder

  3. Include the files to the project

  4. Select the fonts and add them to the solution

  5. Set BuildAction: Resource and Copy To Output Directory: Do not copy. Your .csproj file should now should have a section like this one:

      <ItemGroup>
       <Resource Include="FontsNotoSans-Bold.ttf" />
       <Resource Include="FontsNotoSans-BoldItalic.ttf" />
       <Resource Include="FontsNotoSans-Italic.ttf" />
       <Resource Include="FontsNotoSans-Regular.ttf" />
       <Resource Include="FontsNotoSansSymbols-Regular.ttf" />
     </ItemGroup>
    
  6. In App.xaml add <FontFamily> Resources. It should look like in the following code sample. Note that the URI doesn't contain the filename when packing with the application.

     <Applicaton ...>
     <Application.Resources>
         <FontFamily x:Key="NotoSans">pack://application:,,,/Fonts/#Noto Sans</FontFamily>
         <FontFamily x:Key="NotoSansSymbols">pack://application:,,,/Fonts/#Noto Sans Symbols</FontFamily>
     </Application.Resources>
     </Application>
    
  7. Apply your Fonts like this:

     <TextBlock x:Name="myTextBlock" Text="foobar" FontFamily="{StaticResource NotoSans}" 
                FontSize="10.0" FontStyle="Normal" FontWeight="Regular" />
    
  8. You can also set the font imperatively:

     myTextBlock.FontFamily = new FontFamily(new Uri("pack://application:,,,/"), "./Fonts/#Noto Sans");
    

Copy to Output Directory

  1. Add a /Fonts folder to your solution.

  2. Add the True Type Fonts (*.ttf) files to that order

  3. Include the files to the project

  4. Select the fonts and add them to the solution

  5. Set BuildAction: Content and Copy To Output Directory: Copy if newer or Copy always. Your .csproj file should now should have a section like this one:

      <ItemGroup>
       <Content Include="FontsNotoSans-Bold.ttf">
         <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
       </Content>
       <Content Include="FontsNotoSans-BoldItalic.ttf">
         <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
       </Content>
       <Content Include="FontsNotoSans-Italic.ttf">
         <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
       </Content>
       <Content Include="FontsNotoSans-Regular.ttf">
         <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
       </Content>
       <Content Include="FontsNotoSansSymbols-Regular.ttf">
         <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
       </Content>
     </ItemGroup>
    
  6. In App.xaml add <FontFamily> Resources. It should look like in the following code sample.

     <Applicaton ...>
     <Application.Resources>
         <FontFamily x:Key="NotoSansRegular">./Fonts/NotoSans-Regular.ttf#Noto Sans</FontFamily>
         <FontFamily x:Key="NotoSansItalic">./Fonts/NotoSans-Italic.ttf#Noto Sans</FontFamily>
         <FontFamily x:Key="NotoSansBold">./Fonts/NotoSans-Bold.ttf#Noto Sans</FontFamily>
         <FontFamily x:Key="NotoSansBoldItalic">./Fonts/NotoSans-BoldItalic.ttf#Noto Sans</FontFamily>
         <FontFamily x:Key="NotoSansSymbols">./Fonts/NotoSans-Regular.ttf#Noto Sans Symbols</FontFamily>
     </Application.Resources>
     </Application>
    
  7. Apply your Fonts like this:

     <TextBlock Text="foobar" FontFamily="{StaticResource NotoSansRegular}" 
                FontSize="10.0" FontStyle="Normal" FontWeight="Regular" />
    

References


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...