absolute URI to font in resource - wpf

I would like use custom font in my WPF app.
I created folder fonts in my project and added .ttf file. Then I setup build action for .ttf file to resource.
When I access to font in XAML via absolute URI it doesnt’t work.
<TextBlock Margin = "5"
FontSize = "50"
FontFamily="pack://application:,,,/fonts/Sketch College.ttf">
Custom font
</TextBlock>
Where is problem?
Because if I use relative URI it works.
<TextBlock Margin = "5"
FontSize = "40"
FontFamily = "./fonts/#Sketch College">
Custom fonts
</TextBlock>
Thanks

WPF applications do not allow you to create a FontFamily object programmatically using "pack:" as part of the absolute uniform resource identifier (URI) reference to a font. For example, "pack://application:,,,/resources/#Pericles Light" is an invalid font reference.
Got it from
http://msdn.microsoft.com/en-us/library/ms753303(v=vs.110).aspx

Related

Import a new font into WPF application MVVM

I have file 'resource.xaml' for all the styles. But I need to add a new font file to my project and re-use it instead of default font. How can I add a new font and use it?
I searched the internet and see some methods. but nothing worked for me.
Thanks in Advance.
when using a custom font you must respect the following syntax:
"/FontPath/FontFileName.ttf#FontName"
for exemple :
<Setter Property="FontFamily" Value="/Fonts/VLADIMIR.TTF#Vladimir Script"/>
where the name of this font is "Vladimir Script".
I've had this problem before, and here's how I did it:
Create a folder in your solution and add all of the font .ttf files in there.
In App.xaml, or a resource dictionary, add the following code:
pack://application:,,,/Path/To/Font/#Name Of Font
Then, you can reference the font resource in your XAML code:
<TextBlock FontFamily="{DynamicResource NameOfResource}" Text="Hello World"/>

using font file in wpf usercontrol

I am using a WPF UserControl inside a Winforms form. I have it working.
I would like the text that I use in the WPF UserControl to use a font that I have as a TTF.
I do not know how to reference this TTF and have the control use it. I am assuming I should load the TTF (this is not an installed font) in the UserControl and tell the control (a label) to use it but I only see reference to the font-family.
Here is how to load a font in wpf from a TTF font file
<TextBlock FontSize="48" FontFamily="/Assets/Fonts/Algeria.TTF#Algeria" FontWeight="Normal">test value</TextBlock>
Use a Resource to store the font...
Make sure the font name is correct ....
from git hub I found the following font
github.com/mozilla/Fira/blob/master/ttf/FiraSans-Medium.ttf. For this font the font would be fira Sans Medium so the Xaml would look like
<Label FontFamily="/Resources/firaSans-Medium.ttf#fira Sans Medium" x:Name="TopText" Content="Memory Disk Registry System Program" FontSize="3" Margin="0,-2,0,0"/>

Cannot find the image by providing the relative path in Silverlight

I'm working on a Silverlight application and I have a hard time setting the relative path of an image in my application.
This is a brief picture of my project directory:
MyProject
L images
L mountain1.jpg
L SpriteObjects
L MountainFactory.cs
L GameScreen.xaml
Originally, I painted an Rectangle using an image brush from the GameScreen.xaml.
<Rectangle Name="rec_bg" HorizontalAlignment="Left" VerticalAlignment="Top" Width="800" Height="600">
<Rectangle.Fill>
<ImageBrush x:Name="ib_bg" ImageSource="./images/mountain1.jpg" ></ImageBrush>
</Rectangle.Fill>
</Rectangle>
This xaml code works, that is it can find the image in the images folder without any problem.
I want to change the image dynamically, and I created a class called MountainFactory.cs. In this class, I have a method with this code:
ImageBrush brush = new ImageBrush();
BitmapImage image = new BitmapImage(new Uri("./images/mountain" + level + ".jpg", UriKind.Relative));
brush.ImageSource = image;
This method will return an image brush which is applied to the rec_bg Rectangle object.
However, this code cannot find the specified image.
Does anyone who know how to fix this?
Thanks.
It depends on how you are embedded the image. I'm assuming you have it set as "Content" so it's not embedded in the DLL but does embed in the XAP. In that case, you simply access it relative to the project root, so you would be using:
"images/mountain..."
(Note, you don't use the ./ in front of it, you are automatically relative to the root of the application). If you have it set as an embedded resource, you'll need to extract it out of the DLL using a stream - I don't recommend that.
Short story boring: try removing the leading ./ and make sure the properties of the Image have it as Content.

How can I set the WPF BitmapImage UriSource property to a relative path?

I'm loading an image in WPF by using the BitmapImage class. My code works perfectly when I give an absolute path for the UriSource but not when I try and use a relative path.
My XAML is:
<Image Width="50" Name="MemberImage" HorizontalAlignment="Left">
<Image.Source>
<BitmapImage DecodePixelWidth="50" UriSource="questionmark.jpg" />
</Image.Source>
</Image>
The questionmark.jpg is added to the project with a Build Action of Resource and Copy to Output Directory set to Copy always. The error I get is "The file [file name] is not part of the project or its 'Build Action' property is not set to 'Resource'". This works when I use an absolute path for the UriSource but that obviously won't do.
How should I be defining my UriSource in the XAML?
I don't think you need to copy the image to output directory once it's in resource.
Correct way to specify is
<BitmapImage
x:Key = "Logo"
UriSource = "pack://application:,,,/ApplicationNamespace;component/Images/App/image.png"
/>
Just replace
ApplicationNamespace with your application namespace
and
Images/App/image.png with your image path in the project
Image files with the following options in properties
Build Action=Content
Copy to Output Directory=Copy if newer
<BitmapImage x:Key="QuestionMark" UriSource="pack://siteoforigin:,,,/questionmark.png"/>
Reference:
Xaml - Bitmap UriSource - absolute path works, relative path does not, why?
I cannot reproduce the problem on my computer:
I add the jpg by choosing Add existing item in the Project menu
I then set its Build Actio to Resource and Copy to Output directory to always.
My XAML is the same.
Works perfectly here, even after moving the .exe and renaming the jpg. Something else must be biting you!

WPF relative path problem

I have created a custom TaskButton control that takes an image and text. The properties are set like this:
<custom:TaskButton Text="Calendar" ImagePath="Images/calendar.png" ... />
My custom control class implements Text and ImagePath properties, and the control template for the custom control (in Themes\Generic.xaml) sets its content like this, using a RelativeSource object to get the image path:
<!-- Button Content -->
<StackPanel>
<Image Source="{Binding Path=ImagePath, RelativeSource={RelativeSource TemplatedParent}}" Width="24" Height="24" Stretch="Fill" Margin="10,0,0,0" />
<TextBlock Text="{TemplateBinding Text}" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Segoe UI" FontWeight="Bold" Margin="6,0,10,0" Foreground="Black" />
</StackPanel>
The control works fine in most cases, but in a particular project, the relative path to the button's image does not get resolved correctly, and the button image is not displayed. Here is what I have figured out so far:
I am entering the path correctly when I use the custom control. If I place an image control on the same design surface with the same relative path, it is resolved correctly.
The problem is with the relative path. If I replace the relative path with an absolute path, the path is resolved correctly and the image is displayed.
As I mentioned above, the control works fine in most cases. The one case where it isn't working is a Prism 2.1 project, where the control is instantiated on a user control in a Prism module. The module is a simple class library, but it has all of the references of a WPF project.
Any idea why the relative path would fail? Thanks in advance for your help.
I finally figured out the problem. It was actually in the C# backing class for my control. I declared an ImagePath property as a string, since that was how I was going to specify the image. Oops--bad call on my part. That property should actually be an ImageSource property, not a string. WPF has a built-in ImageSourceConverter class that will resolve the path and return the specified image. So, I simply changed the property name from ImagePath to Image, and changed its type from string to ImageSource. That solved the problem.
Thanks to Aviad P. for taking a crack at this. It was unsolvable without the C# code showing the property declarations. I'll post all code and markup next time.

Resources