Silverlight WriteableBitmap无法正确转换自定义字体

时间:2012-09-06 09:23:04

标签: silverlight canvas embedded-fonts writeablebitmap

我们使用自定义字体来表示我们在图表上绘制的道路标志和形状。

最初我将所有内容绘制到画布上,画面工作正常,但水平滚动速度很慢。

然后我通过使用WriteableBitmap将画布转换为图像来修复滚动问题,如下所示:

#if SILVERLIGHT
    ImageSource ConvertCanvas(Canvas canvas) {

        WriteableBitmap Source = new WriteableBitmap(canvas, /* transform = */ null);

        Source.Invalidate();

        return Source;
    }
#endif

此转换适用于画布上的大多数元素,但使用自定义字体的TextBlocks除外。

因此,我没有使用正确的“自定义字体”图像(字符),而是获得正常的Arial字符,例如“p”,“q”等......

如果我再次显示画布而不是转换后的图像,那么我可以确认自定义字体是否正常工作并正确呈现文本块,因此它似乎不是加载问题...

请帮助或提供一些我可以研究的指示......

提前谢谢

编辑:

好的,我找到了一个解决方案here 1.创建在进行转换之前使用该字体的隐藏文本块,或 2.创建FontSource。

我使用了第一个,因为它现在更简单。

在控件内,我添加了以下内容:

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

0 个答案:

没有答案