Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write text on an image in C#

Tags:

c#

text

image

draw

I have the following problem. I want to make some graphics in bitmap image like bond form

i can write a text in image
but i will write more text in various positions

Bitmap a = new Bitmap(@"path\picture.bmp");

using(Graphics g = Graphics.FromImage(a))
{
    g.DrawString(....); // requires font, brush etc
}

How can I write text and save it, and write another text in saved image.

like image 335
Mr Alemi Avatar asked Jul 26 '11 07:07

Mr Alemi


4 Answers

To draw multiple strings, call graphics.DrawString multiple times. You can specify the location of the drawn string. This example we will draw two strings "Hello", "Word" ("Hello" in blue color upfront "Word" in red color):

string firstText = "Hello";
string secondText = "World";

PointF firstLocation = new PointF(10f, 10f);
PointF secondLocation = new PointF(10f, 50f);

string imageFilePath = @"path\picture.bmp"
Bitmap bitmap = (Bitmap)Image.FromFile(imageFilePath);//load the image file

using(Graphics graphics = Graphics.FromImage(bitmap))
{
    using (Font arialFont =  new Font("Arial", 10))
    {
        graphics.DrawString(firstText, arialFont, Brushes.Blue, firstLocation);
        graphics.DrawString(secondText, arialFont, Brushes.Red, secondLocation);
    }
}

bitmap.Save(imageFilePath);//save the image file

Edit: "I Add a load and save code".

You can open the bitmap file any time Image.FromFile, and draw a new text on it using the above code. and then save the image file bitmap.Save

like image 172
Jalal Said Avatar answered Nov 10 '22 13:11

Jalal Said


Here's an example of a call to Graphics.DrawString, taken from here:

g.DrawString("My\nText", new Font("Tahoma", 40), Brushes.White, new PointF(0, 0));

It obviously relys on having a font called Tahoma installed.

The Brushes class has many built-in brushes.

See also, the MSDN page for Graphics.DrawString.

like image 33
George Duckett Avatar answered Nov 10 '22 14:11

George Duckett


To save changes to the same file, I had to combine Jalal Said's answer and NSGaga's answer on this question. You need to create a new Bitmap object based on the old one, dispose old Bitmap object, then save using the new object:

string firstText = "Hello";
string secondText = "World";

PointF firstLocation = new PointF(10f, 10f);
PointF secondLocation = new PointF(10f, 50f);

string imageFilePath = @"path\picture.bmp";

Bitmap newBitmap;
using (var bitmap = (Bitmap)Image.FromFile(imageFilePath))//load the image file
{
    using(Graphics graphics = Graphics.FromImage(bitmap))
    {
        using (Font arialFont =  new Font("Arial", 10))
        {
            graphics.DrawString(firstText, arialFont, Brushes.Blue, firstLocation);
            graphics.DrawString(secondText, arialFont, Brushes.Red, secondLocation);
        }
    }
    newBitmap = new Bitmap(bitmap);
}

newBitmap.Save(imageFilePath);//save the image file
newBitmap.Dispose();
like image 6
Ibrahim Avatar answered Nov 10 '22 14:11

Ibrahim


    public string imageFilePath = null;
    public string textOnImage = null;

    public Image baseImage;
    public Image modifiedImage;

    public int xcoOrdinate = 0;
    public int ycoOrdinate = 0;

    public Form1()
    {
        InitializeComponent();
    }

    private void buttonLoadImage_Click(object sender, EventArgs e)
    {
        try
        {
            OpenFileDialog uploadfileDialog = new OpenFileDialog();
            uploadfileDialog.Filter = "All Files (*.*)|*.*";
            uploadfileDialog.Multiselect = false;

            if (uploadfileDialog.ShowDialog() == DialogResult.OK)
            {
                imageFilePath = uploadfileDialog.FileName;
            }

            baseImage = Image.FromFile(imageFilePath);
            modifiedImage = (Image)baseImage.Clone();
            pictureBoxToShowPic.Image = baseImage;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Source + " : " + ex.Message);
        }            
    }

    public void paint()
    {
        try
        {
            Graphics g = Graphics.FromImage(modifiedImage);
            using (Font myfont = new Font("Arial", 14))
            {
                var format = new StringFormat
                {
                    Alignment = StringAlignment.Center,
                    LineAlignment = StringAlignment.Center
                };

                g.DrawString(textOnImage, myfont, Brushes.Black, new Point(xcoOrdinate, ycoOrdinate), format);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Source + " : " + ex.Message);
        }
    }

    private void buttonAddText_Click(object sender, EventArgs e)
    {
        try
        {
            textOnImage = textBoxWriteText.Text;
            paint();
            pictureBoxToShowPic.Image = modifiedImage;
            pictureBoxToShowPic.Refresh();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Source + " : " + ex.Message);
        }
    }

    private void pictureBoxToShowPic_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        try
        {
            xcoOrdinate = e.X;
            ycoOrdinate = e.Y;
        }            
        catch (Exception ex)
        {
            MessageBox.Show(ex.Source + " : " + ex.Message);
        }
    }

    private void buttonSaveImage_Click(object sender, EventArgs e)
    {
        try
        {
            SaveFileDialog savefileDialog = new SaveFileDialog();

            savefileDialog.Filter = "Images|*.jpg ; *.png ; *.bmp";

            if (savefileDialog.ShowDialog() == DialogResult.OK)
            {
                imageFilePath = savefileDialog.FileName;
            }

            modifiedImage.Save(imageFilePath);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Source + " : " + ex.Message);
        }
    }
like image 1
Ahsan Habib Shuvo Avatar answered Nov 10 '22 12:11

Ahsan Habib Shuvo