Hey, I am trying to create a BarCode input box. On it a textblock is showing a preview of what the text input will look like in the specified Barcode type.
I have a bunch of .TTF files that are the barcode fonts, they have been used in a WinForms app so I am sure they are correct. I try to load them to memory (don't want to install them) using:
sBarCodeFonts = new PrivateFontCollection();
unsafe
{
fixed (byte* p = Resources.Code39)
{
IntPtr MyIntPtr = (IntPtr)p;
sBarCodeFonts.AddMemoryFont(MyIntPtr, Resources.Code39.Length);
}
fixed (byte* p = Resources.Code128b)
{
IntPtr MyIntPtr = (IntPtr)p;
sBarCodeFonts.AddMemoryFont(MyIntPtr, Resources.Code128b.Length);
}
fixed (byte* p = Resources.i2of5)
{
IntPtr MyIntPtr = (IntPtr)p;
sBarCodeFonts.AddMemoryFont(MyIntPtr, Resources.i2of5.Length);
}
fixed (byte* p = Resources.ean13)
{
IntPtr MyIntPtr = (IntPtr)p;
sBarCodeFonts.AddMemoryFont(MyIntPtr, Resources.ean13.Length);
}
}
This seems to load the PrivateFontCollection correctly, quickwatch indicates so.
In the Barcode class, I have a MyFontFamily property, which contains the System.Media.FontFamily that corresponds to the loaded file.
This property is loaded like this:
MyFontFamily = new System.Windows.Media.FontFamily(sBarCodeFonts.Families[0].Name);
And it seems to be loaded correctly as well.
Then, I have a Barcode object, and I'm using a TextBlock to display it's text, using it's FontFamily:
<TextBlock Text="{Binding Path=MyBarcode.TextContent, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}, Mode=OneWay}"
FontFamily="{Binding Path=MyBarcode.MyFontFamily, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}, Mode=OneWay}"
Name="txt"
Grid.Row="2" />
The TextBlock displays the text using a default font every time. I've debugged and the FontFamily is correctly set to one of the loaded ones in the previous C# code.
Any clues?
Thanks.
EDIT:
Trying to simplify the problem, I've created a very quick and dirty test app to load the TTF and show it, this is the only code (besides the XAML with only a grid):
System.Windows.Media.FontFamily lFamily = new System.Windows.Media.FontFamily(new Uri(#"E:\Prototypes\TestApp\Resources\Code128b.ttf", UriKind.Absolute), "Code128bWin");
TextBlock lTextBlock = new TextBlock();
lTextBlock.Text = "jsldkasjLKJOSDIFUEWR32849792837.,ยด` ";
lTextBlock.FontFamily = lFamily;
lTextBlock.FontSize = 50.0;
grid.Children.Add(lTextBlock);
And it still shows the default font.
<FontFamily x:Key="MyFont">/WpfTest;component/Resources/#Airplanes in the Night Sky</FontFamily>
<TextBlock FontFamily="{StaticResource MyFont}">Hello world</TextBlock>
Given that when you open the .ttf the "Font name: .." is "Airplanes in the Night Sky" and your project namespace is "WpfTest" and you dragged the font into a folder called "Resources"
I don't know about the method you're using, but I've successfully embedded fonts in a WPF application before by adding the files to the project as resources, and then creating a WPF/XAML resource in markup to reference the font file (using a pack Uri). This doesn't require the fonts to be installed on the machine - they're embedded in your .exe or a .dll file.
I did not find any answer for exactly this.
But I found a solution that I did not see on Internet.
I followed a recommendation of doing a folder and marking all the files inside as Resources.
But I needed to enumerate them, and that was my main problem, because I need to load all to my screen without recording the name somewhere. I just want to simple drop another font to that folder and list it.
I found this as a solution to list all the files inside my resources/fonts folder
Fonts.GetFontFamilies(new Uri("pack://application:,,,/resources/fonts/#"))
I expect it to help you organize your fonts.
Related
I put crypto.png image into drawable folder under Resources in Android Project.
After I right click on the image -> Build Action -> Embedded Resource.
In MainPage.xaml on the main project I try to load the image like that:
<Image x:Name="HeadImage"
WidthRequest="100"
HeightRequest="100"
MinimumHeightRequest="100"
MinimumWidthRequest="100"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
Aspect="AspectFit"
Source="crypto.png"/>
But the image not load.
I try a second method like this in the c# code:
var HeadImage = new Image { Aspect = Aspect.AspectFit };
HeadImage.Source = ImageSource.FromFile("crypto.png");
And this method not worked again..
You need to let your image in drawable folder as Android Resource instead of Embedded Resource, and to use it, you need to:
HeadImage.Source = "crypto.png";
Also, before set the image, is a good practice to remove MinimumWidthRequest and MinimumHeightRequest. This is safe ONLY if you are sure the image have this minimum size. Otherwise, your image will not appear.
To know more about image in Xamarin, see here.
And to understand the difference between the ways of set an ImageSource see here.
I am trying to have a label in my WPF-Window that has a special font that is probably not installed on all computers.
This is currently my xaml-code for the label:
<Label x:Name="ping" Content="00" FontFamily="pack://application:,,,/resources/#visitor" HorizontalAlignment="Left" Margin="165,10,0,0" VerticalAlignment="Top"/>
And here is my project hierarchy:
But why doesn't the font appear in the application/on the label?
Sorry to drag up an old thread, but I found the solution to this. While the font filename is visitor1, FontFamily doesn't look for this and instead looks for the font name. Open the font file and you'll see the name is actually "Visitor TT1 BRK". Use this in your code and it will work:
FontFamily="CustomFonts\#Visitor TT1 BRK"
I am trying to display an image on a WPF page. The code is shown below:
<Grid>
<TextBlock Margin="32,332,395,74" Cursor="None">
Click to <Hyperlink NavigateUri="TestWindow.xaml">sign a document</Hyperlink>
</TextBlock>
<Image HorizontalAlignment="Left" Height="151" Margin="32,96,0,0" VerticalAlignment="Top" Width="179"
Source="signature.png" Visibility="Visible" Name="signImage"/>
The problem is the image shows in WPF designer but when I run the program, the image does not display on the page.
Note the image is displayed on a PAGE not a WINDOW
Another option is to use a resource, e.g. in your app.xaml:
<Application.Resources>
<BitmapImage x:Key="signatureSrc" UriSource="/MyProject;component/ImageFolderIfThereIsOne/signature.png" />
</Application.Resources>
and use it like this
<Image Height="100" VerticalAlignment="Top" Width="100" Source="{StaticResource signatureSrc}" />
Try this out PackURIs
Image finalImage = new Image();
finalImage.Width = 80;
...
BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new Uri("pack://application:,,,/AssemblyName;component/Resources/logo.png");
logo.EndInit();
...
finalImage.Source = logo;
The URI is broken out into parts:
Authority: application:///
Path: The name of a resource file that is compiled into a referenced assembly. The path must conform to the following format: AssemblyShortName[;Version][;PublicKey];component/Path
AssemblyShortName: the short name for the referenced assembly.
;Version [optional]: the version of the referenced assembly that
contains the resource file. This is used when two or more referenced
assemblies with the same short name are loaded.
;PublicKey [optional]: the public key that was used to sign the
referenced assembly. This is used when two or more referenced
assemblies with the same short name are loaded.
;component: specifies that the assembly being referred to is
referenced from the local assembly.
/Path: the name of the resource file, including its path, relative to
the root of the referenced assembly's project folder.
The three slashes after application: have to be replaced with commas:
Note: The authority component of a pack URI is an embedded URI that
points to a package and must conform to RFC 2396. Additionally, the
"/" character must be replaced with the "," character, and reserved
characters such as "%" and "?" must be escaped. See the OPC for
details.
And of course, make sure you set the build action on your image to Resource.
Frustrated with this myself, I just figured out the the page uses a relative location for the image. In my case, the image was in the folder "Graphics/logo.jpg", and the page was in the folder "View/Setting/About". I updated the Source of the image to ../../Graphics/logo.jpg and it worked.
I have a WP7 application displaying 3 thumbnails at the bottom and a big one at the center of the screen. Upon clicking on each thumbnail, a method in the xaml.cs file is triggered replacing the source of the big image to display an enlarged version of the thumbnail.
Now, I added a textbox at the bottom of the big image that would display text describing each image. Such text is stored as separate strings in a RESX file for each locale (en, fr).
In the existing 3 methods that update the big image upon clicking on each of the 3 thumbnails, I want to add a line to also update the text of the textbox, displaying the correct string for the current locale of Windows Phone 7.
private void thumb1_Click(object sender, RoutedEventArgs e)
{
Uri uri = new Uri("/myApp;component/Images/Dialog%20-%20Blocked%20Sites.png", UriKind.Relative);
ImageSource newSource = new System.Windows.Media.Imaging.BitmapImage(uri);
imgTarget.Source = newSource;
imgDesc.Text = "placeholderDesc1";
}
I know how to use binding to display localized text from the resx files in the XAML but I don't know the syntax in the xaml.cs code file. Could you please assist?
<TextBlock Height="67" Name="appDesc" Style="{StaticResource PhoneTextTitle1Style}" Text="{Binding Path=LocalizedResources.appDesc, Source={StaticResource LocalizedStrings}}" FontSize="22" TextWrapping="Wrap" />
Many thanks.
Try
AppResources.appDesc
Nothing more
How do I set a PictureBox image to an image from my resources?
(I tried this without success: pictuerbox.Image = "img_location";)
If you loaded the resource using the visual studio UI, then you should be able to do this:
picturebox.Image = project.Properties.Resources.imgfromresource
Ken has the right solution, but you don't want to add the picturebox.Image.Load() member method.
If you do it with a Load and the ImageLocation is not set, it will fail with a "Image Location must be set" exception. If you use the picturebox.Refresh() member method, it works without the exception.
Completed code below:
public void showAnimatedPictureBox(PictureBox thePicture)
{
thePicture.Image = Properties.Resources.hamster;
thePicture.Refresh();
thePicture.Visible = true;
}
It is invoked as:
showAnimatedPictureBox( myPictureBox );
My XAML looks like:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:winForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="myApp.MainWindow"
Title="myApp" Height="679.079" Width="986">
<StackPanel Width="136" Height="Auto" Background="WhiteSmoke" x:Name="statusPanel">
<wfi:WindowsFormsHost>
<winForms:PictureBox x:Name="myPictureBox">
</winForms:PictureBox>
</wfi:WindowsFormsHost>
<Label x:Name="myLabel" Content="myLabel" Margin="10,3,10,5" FontSize="20" FontWeight="Bold" Visibility="Hidden"/>
</StackPanel>
</Window>
I realize this is an old post, but loading the image directly from a resource is was extremely unclear on Microsoft's site, and this was the (partial) solution I came to. Hope it helps someone!
Ok...so first you need to import in your project the image
1)Select the picturebox in Form Design
2)Open PictureBox Tasks (it's the little arrow pinted to right on the edge on the picturebox)
3)Click on "Choose image..."
4)Select the second option "Project resource file:" (this option will create a folder called "Resources" which you can acces with Properties.Resources)
5)Click on import and select your image from your computer (now a copy of the image with the same name as the image will be sent in Resources folder created at step 4)
6)Click on ok
Now the image is in your project and you can use it with Properties command.Just type this code when you want to change the picture from picturebox:
pictureBox1.Image = Properties.Resources.myimage;
Note: myimage represent the name of the image...after typing the dot after Resources,in your options it will be your imported image file
try the following:
myPictureBox.Image = global::mynamespace.Properties.Resources.photo1;
and replace namespace with your project namespace
You can use a ResourceManager to load the image.
See the following link:
http://www.java2s.com/Code/CSharp/Development-Class/Saveandloadimagefromresourcefile.htm
You must specify the full path of the resource file as the name of 'image within the resources of your application, see example below.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PictureBox1.Image = My.Resources.Chrysanthemum
End Sub
In the path assigned to the Image property after MyResources specify the name of the resource.
But before you do whatever you have to import in the resource section of your application from an image file exists or it can create your own.
Bye