5

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 ?

Yanshof
  • 9,659
  • 21
  • 95
  • 195

1 Answers1

7

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();
}
Dan Auclair
  • 3,607
  • 25
  • 32
  • 1
    I'm not using Silverlight and I'm getting these errors: Error 1 'System.Windows.Media.Imaging.WriteableBitmap' does not contain a definition for 'Render' and no extension method 'Render' accepting a first argument of type 'System.Windows.Media.Imaging.WriteableBitmap' could be found and Error 2 'System.Windows.Media.Imaging.WriteableBitmap' does not contain a definition for 'Invalidate' and no extension method 'Invalidate' accepting a first argument of type 'System.Windows.Media.Imaging.WriteableBitmap' – Is this because I'm not using Silverlight? – zetar Oct 17 '13 at 18:48
  • 1
    @zetar you need the WriteableBitmapEx library. http://writeablebitmapex.codeplex.com – David Sykes May 20 '14 at 14:31