Pieter
Pieter

Reputation: 111

Windows Phone 8.1 - Add text to Image

I've been struggling with this for a day now and it's really frustrating because it is something that you would expect to be so simple, yet, there's a lot of questions regarding this on the net.

How do you add text to a image in a Windows Phone 8.1 Project.

What I've seen thus far is create a WriteableBitmap, some examples using the constructor which takes a BitmapImage. Well, the only constructor I have take a width and height.

Then you can add a textblock to the writeableBitmap using .Render(UIElement, … Well, I don't have a .Render method on the WriteableBitmap instance.

I'm also using the WriteableBitmapEx library as well.

I'm using Visual Studio 2015, Ccommunity edition and it is a Windows Phone 8.1 project.

Any help would be appreciated.

Upvotes: 2

Views: 79

Answers (1)

Jogy
Jogy

Reputation: 2475

Here is a simple example how to render an arbitrary UI element named uielement to a bitmap and then copying it to a WriteableBitmap:

  var renderBitmap = new RenderTargetBitmap();
  await renderBitmap.RenderAsync(uielement);
  var buf = await renderBitmap.GetPixelsAsync();
  bitmap = new WriteableBitmap(renderBitmap.PixelWidth, renderBitmap.PixelHeight);
  using (var stream1 = buf.AsStream())
  using (var stream2 = bitmap.PixelBuffer.AsStream())
  {
    await stream1.CopyToAsync(stream2);
  }

Upvotes: 3

Related Questions