Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8373685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:44:29+00:00 2026-06-09T14:44:29+00:00

So I was doing this in WinForms .NET 3.5… I am now using WPF

  • 0

So I was doing this in WinForms .NET 3.5… I am now using WPF .NET 4.0… and I cant figure out how to do it.

This is what I was doing in Windows .NET 3.5

using (Bitmap eventImg = new Bitmap("input.png"))
{
    Graphics eventGfx = Graphics.FromImage(eventImg);

    buildText(eventGfx, this.event1.Text);

    eventImg.Save("output.png", ImageFormat.Png);
    eventGfx.Dispose();
}

The above code took the existing image at “input.png”, created a new image from it, wrote text from it and then saved the new image at “output.png”. Text was written using the following function:

private void buildText(Graphics graphic, string text)
{
    if (text.Length == 0) { return; }

    FontStyle weight = FontStyle.Regular;

    switch (this.font_style)
    {
        case "regular":     weight = FontStyle.Regular;     break;
        case "bold":        weight = FontStyle.Bold;        break;
        case "italic":      weight = FontStyle.Italic;      break;
        case "underline":   weight = FontStyle.Underline;   break;
        case "strikeout":   weight = FontStyle.Strikeout;   break;
    }

    using (Font font = new Font(this.font_family, this.font_size, weight, GraphicsUnit.Pixel))
    {
        Rectangle rect = new Rectangle(this.left, this.top, this.width, this.height);
        Brush brush = new SolidBrush(Color.FromArgb(this.font_color));

        StringFormat format = new StringFormat();

        switch (this.align_x)
        {
            case "left":    format.Alignment = StringAlignment.Near;     break;
            case "right":   format.Alignment = StringAlignment.Far;      break;
            default:        format.Alignment = StringAlignment.Center;   break;
        }

        switch (this.align_y)
        {
            case "top":     format.LineAlignment = StringAlignment.Near;    break;
            case "bottom":  format.LineAlignment = StringAlignment.Far;     break;
            default:        format.LineAlignment = StringAlignment.Center;  break;
        }

        graphic.TextRenderingHint = TextRenderingHint.AntiAlias;
        graphic.DrawString(text, font, brush, rect, format);
    }
}

However, since System.Drawing does not exist in WPF .NET 4.0, I can’t use these functions anymore. How would I do what I am trying to do in WPF .NET 4.0? I’ve gotten as far as the code below in order to do the first step of making an image based on the old image:

using (var fileStream = new FileStream(@"z:\ouput.png", FileMode.Create))
{
    BitmapEncoder encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(new Uri(@"z:\input.png")));
    encoder.Save(fileStream);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:44:31+00:00Added an answer on June 9, 2026 at 2:44 pm

    Having read answers and comments here I thought you might appreciate a more comprehensive solution. Here is a little method that does the job:

    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 a 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 the text horizontally and vertically aligned relative to a certain rectangle, you might replace the position parameter by that rectangle and two alignment parameters and calculate the text position like this:

    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 might use the method like this:

    WriteTextToImage(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg", "Desert.png", text,
        new Rect(80, 50, 430, 200),
        HorizontalAlignment.Center, VerticalAlignment.Center);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im doing a project with C# winforms. This project is composed by: alt text
I hope I am doing this correctly.. Using this code I am grabbing records
I'm creating a WinForms control in C# (using VS2008, .net 3.5) which allows text
Specification C# Distributed Application. Client/Server design. Client (Winforms), Server (Windows Service), Communication via .Net
This is on .Net 4, full framework. I'm trying to make a simple winforms
I have been doing ASP.NET / C# development for several years now. I have
I'm writing a small business Winforms application in C#, .NET 4.0, using Entity Framework
I'm trying out Ninject with a winforms app (basically a sketch, I'm using it
I'm working on a code (WinForms C# NET 3.5) that involves using unrar. [DllImport(UNRAR64.DLL)]
Im doing this project where i need to download files through a webservice (images,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.