After reading the answers and comments, I thought you could appreciate a more comprehensive solution. Here is a small method that does this work:
public static void WriteTextToImage(string inputFile, string outputFile, FormattedText text, Point position) { BitmapImage bitmap = new BitmapImage(new Uri(inputFile)); // inputFile must be absolute path DrawingVisual visual = new DrawingVisual(); using (DrawingContext dc = visual.RenderOpen()) { dc.DrawImage(bitmap, new Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight)); dc.DrawText(text, position); } RenderTargetBitmap target = new RenderTargetBitmap(bitmap.PixelWidth, bitmap.PixelHeight, bitmap.DpiX, bitmap.DpiY, PixelFormats.Default); target.Render(visual); BitmapEncoder encoder = null; switch (Path.GetExtension(outputFile)) { case ".png": encoder = new PngBitmapEncoder(); break; // more encoders here } if (encoder != null) { encoder.Frames.Add(BitmapFrame.Create(target)); using (FileStream outputStream = new FileStream(outputFile, FileMode.Create)) { encoder.Save(outputStream); } } }
You would use this method with a FormattedText object and position:
FormattedText text = new FormattedText( "Hello", CultureInfo.InvariantCulture, FlowDirection.LeftToRight, new Typeface("Segeo UI"), 20, Brushes.Red); WriteTextToImage( @"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg", "Desert.png", text, new Point(10, 10));
EDIT: if you want to draw text horizontally and vertically with respect to a specific rectangle, you can replace the position parameter with that rectangle and two alignment parameters and calculate the text position as follows:
public static void WriteTextToImage(string inputFile, string outputFile, FormattedText text, Rect textRect, HorizontalAlignment hAlign, VerticalAlignment vAlign) { BitmapImage bitmap = new BitmapImage(new Uri(inputFile)); DrawingVisual visual = new DrawingVisual(); Point position = textRect.Location; switch (hAlign) { case HorizontalAlignment.Center: position.X += (textRect.Width - text.Width) / 2; break; case HorizontalAlignment.Right: position.X += textRect.Width - text.Width; break; } switch (vAlign) { case VerticalAlignment.Center: position.Y += (textRect.Height - text.Height) / 2; break; case VerticalAlignment.Bottom: position.Y += textRect.Height - text.Height; break; } using (DrawingContext dc = visual.RenderOpen()) { dc.DrawImage(bitmap, new Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight)); dc.DrawText(text, position); } RenderTargetBitmap target = new RenderTargetBitmap(bitmap.PixelWidth, bitmap.PixelHeight, bitmap.DpiX, bitmap.DpiY, PixelFormats.Default); target.Render(visual); BitmapEncoder encoder = null; switch (Path.GetExtension(outputFile)) { case ".png": encoder = new PngBitmapEncoder(); break; case ".jpg": encoder = new JpegBitmapEncoder(); break; } if (encoder != null) { encoder.Frames.Add(BitmapFrame.Create(target)); using (FileStream outputStream = new FileStream(outputFile, FileMode.Create)) { encoder.Save(outputStream); } } }
Now you can use the method as follows:
WriteTextToImage(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg", "Desert.png", text, new Rect(80, 50, 430, 200), HorizontalAlignment.Center, VerticalAlignment.Center);