Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Having read answers and comments here I thought you might appreciate a more comprehensive solution. Here is a little method that does the job:</p> <pre><code>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); } } } </code></pre> <p>You would use this method with a <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.formattedtext.aspx">FormattedText</a> object and a position:</p> <pre><code>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)); </code></pre> <p>EDIT: If you want to draw the text horizontally and vertically aligned relative to a certain rectangle, you might replace the <code>position</code> parameter by that rectangle and two alignment parameters and calculate the text position like this:</p> <pre><code>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); } } } </code></pre> <p>Now you might use the method like this:</p> <pre><code>WriteTextToImage(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg", "Desert.png", text, new Rect(80, 50, 430, 200), HorizontalAlignment.Center, VerticalAlignment.Center); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload