如何在 WriteableBitmap 上写字符串? [英] How can i write string on WriteableBitmap?

查看:23
本文介绍了如何在 WriteableBitmap 上写字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WriteableBitmap 并且希望用户能够在它上面进行绘制,就好像它是一个简单的位图一样.

I have a WriteableBitmap and would like the user to be able to draw over it as if it was an simple bitmap.

我该怎么做?

推荐答案

您可以在代码中设置 TextBlock 控件,使用字符串设置 Text 属性,并使用该 TextBlock 调用 WritableBitmap 的 Render() 方法.TextBlock 永远不必在可视化树上,但您​​必须在位图上调用 Invalidate() 之后才能显示文本.

You can set up a TextBlock control in code, set the Text property with the string, and call the Render() method of the WritableBitmap with that TextBlock. The TextBlock never has to be on the visual tree, but you will have to call Invalidate() on the bitmap after to get the text to show up.

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();
}

这篇关于如何在 WriteableBitmap 上写字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆