2

I'm trying to develop something on WP7 (XAML not XNA) and want to be able to draw text onto an image and then save that image with the text on it. Is there a library or function that already exists that does this or would I need to implement my own solution to draw every character?

n00b
  • 5,843
  • 11
  • 52
  • 82

2 Answers2

3

There is no API in WP7 for rendering text to a bitmap image. The WP7 API for manipulating bitmap images is WriteableBitmap, which gives you an array of pixels and nothing more!

There is a good codeplex project WriteableBitmapEx, which adds various drawing extension methods, but not text rendering.

You can however place text above an image, for example ...

<Grid>
  <Image Source="myImage.png"/>
  <TextBlock Text="Overlay text"/>
<Grid>

This will render the text above the image.

You can also use a WriteableBitmap to 'capture' part of the visual tree into a bitmap, see my blog post for examples. The route you take really depends on your requirements.

ColinE
  • 68,894
  • 15
  • 164
  • 232
  • Thanks Colin. Unfortunately the image won't be loaded inside the XAML so I won't be able to overlay the text on top of the image. I guess I'm going to have to look into a way of rendering text onto an image. – n00b Sep 20 '11 at 07:35
  • @n00b you could still create this in code. Grid g = new Grid(); g.Children.Add(new Image()...) should act the same way. – William Melani Sep 20 '11 at 20:51
  • @willmel I didn't specify in my original question but I'd like to save the image with the text on it. Unfortunately using grids will not allow me to save that text. – n00b Sep 21 '11 at 01:58
  • @n00b check this out: http://www.smartypantscoding.com/content/rendering-text-writeablebitmap-silverlight-3s-bitmap-api looks like it might help do what you need. – William Melani Sep 21 '11 at 05:47
  • @willmel Thanks willmel! I was actually on the same path a few hours ago and it seems to be the solution! Thanks! – n00b Sep 21 '11 at 06:09
  • @ColinE Hi Colin, unfortunately there is a way of writing text onto a WriteableBitmap and your answer was a little misleading. – n00b Sep 21 '11 at 06:15
0

I found a way of doing this by adding a Textblock onto the WriteableBitmap. This link gives you an example of how it can be achieved.

How can i write string on WriteableBitmap?

Community
  • 1
  • 1
n00b
  • 5,843
  • 11
  • 52
  • 82