Note that there are some explanatory texts on larger screens.

plurals
  1. POSilverlight WriteableBitmap not converting custom font correctly
    primarykey
    data
    text
    <p>We use custom fonts to represent road signs and shapes that we plot on a graph.</p> <p>Initially I plotted everything to a canvas, which worked fine, but was slow to scroll horizontally.</p> <p>Then I fixed the scrolling issue by converting the canvas to a image using the WriteableBitmap, like this:</p> <pre><code>#if SILVERLIGHT ImageSource ConvertCanvas(Canvas canvas) { WriteableBitmap Source = new WriteableBitmap(canvas, /* transform = */ null); Source.Invalidate(); return Source; } #endif </code></pre> <p>This conversion works fine for most of the elements on the canvas, except the TextBlocks that use the custom font.</p> <p>So, instead of the proper "custom font" images (characters), I get normal Arial characters, like 'p', 'q', etc...</p> <p>If I display the canvas again instead the converted image then I can confirm that the custom font works and renders the textblock correctly, so it does not seem to be a loading issue...</p> <p>Please help or give some pointers I can look into...</p> <p>Thank you in advance</p> <p>EDIT:</p> <p>OK, I found a solution <a href="http://social.msdn.microsoft.com/forums/en-us/wpdevelop/thread/abbbf3c1-7970-46f9-9560-661965ae33ed/" rel="nofollow">here</a> which boils down to either 1. creating a hidden textblock that use the font before doing the conversion, or 2. creating a FontSource. </p> <p>I used the first because it was simpler for now.</p> <p>Inside the control, I added the following:</p> <pre><code>void Grid_Loaded(object sender, RoutedEventArgs e) { #if SILVERLIGHT Grid Grid = (Grid)sender; AddFontLoaderTextBox(Grid, "Signs Road Features"); AddFontLoaderTextBox(Grid, "Signs G Old"); AddFontLoaderTextBox(Grid, "Signs G"); AddFontLoaderTextBox(Grid, "Signs G1"); AddFontLoaderTextBox(Grid, "Signs G2"); AddFontLoaderTextBox(Grid, "Signs G3"); AddFontLoaderTextBox(Grid, "Signs Info"); AddFontLoaderTextBox(Grid, "Signs Regulatory"); AddFontLoaderTextBox(Grid, "Signs Regulatory1"); AddFontLoaderTextBox(Grid, "Road Manager"); AddFontLoaderTextBox(Grid, "Signs Temporary"); AddFontLoaderTextBox(Grid, "Road Manager"); AddFontLoaderTextBox(Grid, "Signs Warning"); AddFontLoaderTextBox(Grid, "Signs Warning1"); #endif } #if SILVERLIGHT void AddFontLoaderTextBox(Grid Grid, string fontName) { TextBlock TextBlock = new TextBlock(); TextBlock.FontFamily = new FontFamily(string.Format( "pack://application:,,,/ITIS.Controls.LinearViewer.Silverlight;component/Fonts/{0}.ttf#{0}", fontName)); TextBlock.Opacity = 0; /* hide the text block, we only load it for the font to be cached */ Grid.SetRowSpan(TextBlock, 3); /* just to ensure the text block doesn't affect the size of the first row */ Grid.Children.Insert(0, TextBlock); /* keep underneath other children */ } #endif </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload