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

  • Home
  • SEARCH
  • 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 7514935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:38:31+00:00 2026-05-30T00:38:31+00:00

I need to be able to draw a text on a png file at

  • 0

I need to be able to draw a text on a png file at runtime.(WinForm app 2010 c#)

Application Starts and toolbar is loaded

Customer is retrieved

At this point when a customer is loaded an icon on the toolbar must be modified and add either a number or letter to left botton corner of this icon.(png file)

I have been playing with a noddy app just to see what I can do,but I am having few issues.

  1. How do I get the path for my png file .(I am hardcoding it in my test)
  2. I am able to draw a text but it’s not displayed.I guess I need to reload the toolbar,no?
  3. When writing on the icon for 2nd time or more it write on top of my previous letter.I want it to remove/clear previous one and just have the new one.

Any suggestions?

My temp code:

 private void button1_Click(object sender, EventArgs e)
    {

        string filename = @"c:\WinFormTest\Resources\column.png";

        var bitmapImage = new Bitmap(Properties.Resources.column);
        bitmapImage.DrawText("A", new Font("Arial", 8), Brushes.Black, new RectangleF(0, 0, 500, 500), filename);

    }
}

public static class ImageExtensions
{
    public static Bitmap DrawText(this Bitmap image,string textToDraw,Font font,Brush brush,RectangleF rectangleF,string filename="")
    {
        if(image==null) throw new ArgumentNullException();
        if(font==null)throw new ArgumentNullException();
        if (brush == null) throw new ArgumentNullException();
        if (rectangleF == null) throw new ArgumentNullException();

        var format = filename.GetImageFormat();
        var newBitmap = new Bitmap(image, image.Width, image.Height);
        using (var graphics = Graphics.FromImage(newBitmap))
        {
            graphics.DrawString(textToDraw, font, brush, rectangleF);
        }
        if (!string.IsNullOrEmpty(filename))
        {newBitmap.Save(filename, format);}
        return newBitmap;
    }
    public static ImageFormat GetImageFormat(this string fileName)
    {
        if (string.IsNullOrEmpty(fileName))
            return ImageFormat.Bmp;
        if (fileName.EndsWith("jpg", StringComparison.InvariantCultureIgnoreCase) || fileName.EndsWith("jpeg", StringComparison.InvariantCultureIgnoreCase))
            return ImageFormat.Jpeg;
        if (fileName.EndsWith("png", StringComparison.InvariantCultureIgnoreCase))
            return ImageFormat.Png;
        if (fileName.EndsWith("tiff", StringComparison.InvariantCultureIgnoreCase))
            return ImageFormat.Tiff;
        if (fileName.EndsWith("ico", StringComparison.InvariantCultureIgnoreCase))
            return ImageFormat.Icon;
        if (fileName.EndsWith("gif", StringComparison.InvariantCultureIgnoreCase))
            return ImageFormat.Gif;
        return ImageFormat.Bmp;
    }


}
  • 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-05-30T00:38:33+00:00Added an answer on May 30, 2026 at 12:38 am

    You can do this at run-time without having to worry about files. If you use embedded resources then you don’t have to worry about file names and clearing the changes from previous sessions and writing to files. When you create a Toolstrip button by default it will use an embedded resource (and you can refer to a shared resource that is used by other forms if you use a resx file in your project). But once you have this, you don’t even need to clone the image (make a new Bitmap) as you are doing. Here’s some much simpler sample code that will update the image in-place:

    public partial class Form1 : Form
    {
       public Form1()
       {
          InitializeComponent();
          ((Bitmap)toolStripButton1.Image).DrawText("B", Font, SystemBrushes.ControlText,
             new RectangleF(new PointF(0, 0), toolStripButton1.Image.Size));
       }
    }
    
    public static class ImageExtensions
    {
       public static void DrawText(this Bitmap image, string stringToDraw, Font font,
          Brush brush, RectangleF rectangleF)
       {
          using (Graphics g = Graphics.FromImage(image))
          {
             g.DrawString(stringToDraw, font, brush, rectangleF);
          }
       }
    }
    

    Since the image is not saved to a file, it is automatically reset each time the program runs. That’s what you want, right?

    If you need to be able to reset the image while the program is still running you can copy the generated code that sets the image in InitializeComponent. For example:

      private void toolStripButton1_Click(object sender, EventArgs e)
      {
         this.toolStripButton1.Image.Dispose();
         System.ComponentModel.ComponentResourceManager resources =
            new System.ComponentModel.ComponentResourceManager(typeof(Form1));
         this.toolStripButton1.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton1.Image")));
         ((Bitmap)toolStripButton1.Image).DrawText("C", Font, SystemBrushes.ControlText,
            new RectangleF(new PointF(0, 0), toolStripButton1.Image.Size));
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to be able to send a file to another website from server-side
In my application, I need to draw JTextArea on certain position on my panel,
I am developping an application where I need to draw a graph on the
I need the equivalent of chls property to be able to draw dotted line
I need to be able to draw selection in a sequence diagram. I have
In the game I'm developing I need this function: being able to draw on
Short Version How can I draw short text labels in an OpenGL mapping application
Using Python I want to be able to draw text at different angles using
I am working on an application where the user is able to draw on
I need to be able to draw a polygon using mouse click locations. Here

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.