如何在WriteableBitmap上写入字符串?

5
我有一个WriteableBitmap,希望用户能够像简单位图一样在其上绘制。如何实现?

你是在谈论如何在现有的WritableBitmap上渲染一串文本吗? - Dan Auclair
是的 - 我需要在其上渲染一串文本。 - Yanshof
1个回答

7

您可以在代码中设置TextBlock控件,使用字符串设置Text属性,并调用WritableBitmap的Render()方法与该TextBlock一起使用。TextBlock不必出现在可视树上,但是在获取文本显示之前,您将需要在位图上调用Invalidate()。

private void RenderString(WriteableBitmap bitmap, string stringToRender)
{
    TextBlock textBlock = new TextBlock();
    textBlock.Text = stringToRender;

    // set font, size, etc. on textBlock

    bitmap.Render(textBlock, null);
    bitmap.Invalidate();
}

1
我没有使用Silverlight,但是我仍然遇到了这些错误:错误1:'System.Windows.Media.Imaging.WriteableBitmap'不包含“Render”的定义,也没有接受类型为'System.Windows.Media.Imaging.WriteableBitmap'的第一个参数的扩展方法'Render';错误2:'System.Windows.Media.Imaging.WriteableBitmap'不包含“Invalidate”的定义,也没有接受类型为'System.Windows.Media.Imaging.WriteableBitmap'的第一个参数的扩展方法'Invalidate'。这是因为我没有使用Silverlight吗? - zetar
1
@zetar 你需要使用WriteableBitmapEx库。http://writeablebitmapex.codeplex.com - David Sykes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接