I\'m using a Kinect sensor to show a video feed on an image by setting the video feed as bitmap source like shown below. But my question is how would I add text to the image
You don't need to draw the text into the image itself. In your XAML just add a TextBlock control at a higher Z order.
You can achieve this using DrawingVisual
and DrawingImage
classes :
var random = new Random();
var pixels = new byte[256 * 256 * 4];
random.NextBytes(pixels);
BitmapSource bitmapSource = BitmapSource.Create(256, 256, 96, 96, PixelFormats.Pbgra32, null, pixels, 256 * 4);
var visual = new DrawingVisual();
using (DrawingContext drawingContext = visual.RenderOpen())
{
drawingContext.DrawImage(bitmapSource, new Rect(0, 0, 256, 256));
drawingContext.DrawText(
new FormattedText("Hi!", CultureInfo.InvariantCulture, FlowDirection.LeftToRight,
new Typeface("Segoe UI"), 32, Brushes.Black), new Point(0, 0));
}
var image = new DrawingImage(visual.Drawing);
Image1.Source = image;
Unfortunately you will have to create a new BitmapSource
as there's currently no way I know of writing text directly to it.
Alternatively you could use WriteableBitmapEx
: https://writeablebitmapex.codeplex.com/
BitmapFactory
(1)Same result but different approach, not sure whether approach 2 is better as it's cumbersome.