Is it possible to add text to bitmap?

Apr 30, 2010 at 8:21 PM

I need to be able to animate a large chunk of text and when I do that with a textblock it consumes waaay too much cpu power to create the animation. Since this animation can last several minutes I don't want to have it heat up the cpu. So looking at alternatives, I noticed that animating an image of the same complexity takes only a fraction of the cpu power, is it possible to add text to the writeablebitmap using your extensions?

Apr 30, 2010 at 8:46 PM

After posting this I realized that I could do this with the base writablebitmap, but performance was not as good as I hoped although still much better.

 

May 3, 2010 at 4:40 PM

How did you manage to do it?  I've been trying for five days to get it to work in Silverlight 3.  In a nutshell, this is what I'm doing, but I can't get text to show up on the bitmap.  For what it's worth, I've seen a lot of references to the Lock and Unlock methods, but according to Intellisense, they don't exist in the WriteableBitmap class.

Label label = new Label();
label.Content = "WHMC";

WriteableBitmap bmp = new WriteableBitmap(120,120);
bmp.Render(label, null);
bmp.Invalidate();

Image image = new Image();
image.Source = bmp;

 

Coordinator
May 3, 2010 at 7:34 PM
You should try to use WriteableBitmap bmp = new WriteableBitmap(label, null); directly. But I think you have to add your label to a container panel (Grid, Canvas, ...) as a child so it's part of the visual tree before you render it.
Aug 6, 2010 at 8:14 PM

I just implemented the following extension method of my own using your above help. Thought I'd share:

    public static void DrawText(this WriteableBitmap wBmp,Point at, string text,double fontSize,Color textColor)
{
TextBlock lbl = new TextBlock();
lbl.Text = text;
lbl.FontSize = fontSize;
lbl.Foreground = new SolidColorBrush(textColor);
WriteableBitmap tBmp = new WriteableBitmap(lbl, null);

        wBmp.Blit(at, tBmp, new Rect(0, 0, tBmp.PixelWidth, tBmp.PixelHeight), Colors.White, System.Windows.Media.Imaging.WriteableBitmapExtensions.BlendMode.Alpha);
}

 

Could potentially Cache the temporary bitmap. You'd have to put a fairly complex system in place to track how often the same thing is drawn and throw away "changing values" or else you'd end up overflowing some memory somewhere.

Maybe  a method like it could be added to the library at-least as a place holder until a better one could be implemented?

BTW I love this project, it's what I've been looking for. I get an itch to make simple games now and then, this gives me all the benefits of the simplicity of GDI + features of SL.