3

I am new to WP7 development and I would like to know how can I write text to image?

First is it possible to do so?

As in GDI we can write text to image as shown below:

Dim pth As New GraphicsPath()
pth.AddString(txtSample.Text, New FontFamily(DropFont.SelectedValue), 0, Integer.Parse(DropFontSize.SelectedValue), New Point(left, top), StringFormat.GenericTypographic)

But in WP7 as I came to know that GDI is not supported.So how Can I do this?

Edit:

I need to select an image from the pictures hub or take a picture using camera and display it in an image control and write some text and save back with a different name.

Any suggestions are most welcome.

coder
  • 13,002
  • 31
  • 112
  • 214

2 Answers2

4

You need to get hold of a WriteableBitmap, which can then be manipulated.

This can be done by either adding a UIElement using the Render method or you can manipulate the pixels directly using the Pixels array.

You probably only need to add TextBlock elements to the bitmap, but if you are curious about pixel manipulation here is how that is done:

I have only experience with pixel manipulation. This is not entirely straight forward, but you access pixel (x, y) in the one-dimensional array by translating y * width + x.

The value is in a format called argb32, ie values for alpha-channel (opacity), red, green and blue. Translation between regular Color and argb32 below:

    int ColorToInt(Color c)
    {
        var argb32 = c.A << 24 | c.R << 16 | c.G << 8 | c.B;
        return argb32;
    }

    Color IntToColor(int argb32)
    {
        const int mask = 0x000000FF;
        byte a, r, g, b;
        a = (byte)((argb32 >> 24) & mask);
        r = (byte)((argb32 >> 16) & mask);
        g = (byte)((argb32 >> 8) & mask);
        b = (byte)(argb32 & mask);
        return Color.FromArgb(a, r, g, b);
    }
faester
  • 14,886
  • 5
  • 45
  • 56
2

Why do you need them embedded in the Image?

You could simply place your image and Text in a Grid such as:

<grid>
   <image source="YourImageSource"/>
   <TextBlock Text="Your Text Here"/>
</grid>

That will overlay your image with Text without having to modify the image so you can use it later. It also provides more freedom with bindings etc as you can bind both to different things and switch them in and out independently.

If you are using XNA this can also be done by manipulating the Pixels of the Texture2D the same way as faester said.

Runewake2
  • 471
  • 1
  • 7
  • 16
  • @Runewake2-I have seen this answer on SO before but that's a bad idea as I dont want to show only the text and I need to do some manipulations such as changing the position of the text.Then the grid dosent fit my purpose. – coder Mar 13 '12 at 08:38
  • @DotNetter If you need to change the position of the text this can be done via setting the TextBlock's Margin to the offset you want (will be relative to the grid's position). This should give you total freedom of the text. Also, if you want to move text then you don't want to use the other idea of embedding the text in the image as regenerating an image is slow... really slow. – Runewake2 Mar 14 '12 at 01:11