How can i write string on WriteableBitmap?

后端 未结 1 887
自闭症患者
自闭症患者 2020-12-10 07:52

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

How can i do it ?

相关标签:
1条回答
  • 2020-12-10 08:42

    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();
    }
    
    0 讨论(0)
提交回复
热议问题