5

In normal C# it is easy to draw to a bitmap using the Grpahics.DrawString() method. Silverlight seems to have done away with Bitmap objects and Graphics is no longer available either. So...How am I meant to manipulate/create a bitmap when using Silverlight? If it helps, I am using Silverlight 3.

Let me tell you what I am doing. I am being given a template, basically a pre-rendered image. The user is then able to select from multiple images and enter the deisred text. I then render it to the image, adjusting size etc... within bounds and centering it in the pre-defined area of the image. If I can calculate the size (as in the MeasureString method) and then draw the string (as in the Graphics.DrawString method) that would be fine. The real question, no matter why I want to be able to do this, is can it be done?

Spenduku
  • 409
  • 4
  • 12
  • 2
    There is alway a worry on the part of those giving advice that it is appropriate to the problem at hand. Not knowing __why__ someone wants to do somehing (esp. something that seems ill-advised on the surface) makes us uncomfortable. Providing the reason __why__ may allow someone to provide a more latteral solution that you may not yet have considered. – AnthonyWJones Feb 10 '10 at 11:46

2 Answers2

3

The question is: why do you want to? Why not just use a TextBlock?

If you are trying to dynamically generate an image, use standard Silverlight/WPF controls (including TextBlock) and render them to a WritableBitmap.

Edit:

Ok, you've updated and expanded, which gives me more to go on. Unfortunately, you're not going to like the answer. First, keep in mind that Silverlight and WPF in general are vector based, and intended to be used as such. Although the Canvas allows you to do pseudo-pixel manipulations, you cannot be nearly as pixel-accurate as old-school GDI. This is a factor of your medium. If you absolutely have to measure things the way you want to measure them, I suggest you build your images on a remote server and transmit them to your Silverlight app.

You can calculate the size on-screen of the text rendered via a TextBlock using the ActualWidth and ActualHeight properties. But it only works on an already rendered control. Something like MeasureString is simply not available in Silverlight. Based on your description of your app, some user interaction could accomplish what you want. The user selects the image, enters the text, and is shown a preview. The user can then adjust the width and height of the various text areas until satisfied, at which point you can take a snapshot using the render method I liked to above.

Randolpho
  • 55,384
  • 17
  • 145
  • 179
  • OK...So how would I do that then? Let me tell you what I am doing. I am being given a template, basically an pre-rendered image. The user is then able to select from multiple images and enter the deisred text. I then render it to the image, adjusting size etc... within bounds and centering it in the pre-defined area of the image. If I can calculate the size (As in the MeasureString method) and then draw the given string (as in the Graphics.DrawString method) using your technique then that would be fine. The real question, no matter why I want to be able to do this, is can it be done? – Spenduku Feb 10 '10 at 00:04
  • @Spencer: you should move that comment into your question. – John Knoeller Feb 10 '10 at 00:16
  • The answer is not what I was hoping for, but it seems to be the only doeable way. So thanks! :) – Spenduku Feb 18 '10 at 22:41
  • Glad I could help. Good luck with it! – Randolpho Feb 18 '10 at 23:13
3

The following may work, its a bit nebulous because I haven't tried yet myself.

The object you are looking for is the WritableBitmap.

You create a Visual tree, for example create your self a Grid or Canvas (you're not adding this to the UI). Add to it the selected image and a TextBlock positioned and sized as you prefer.

Create a new WritableBitmap either of a specific size or using the selected image to initialize it.

Use the WritableBitmap Render method passing the above root Grid or Canvas to it.

Now you have a bitmap which you should able to use to do whatever its you needed to do that required all this hoop jumping in the first place.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306