Write text on an image in C#

后端 未结 5 1216
梦毁少年i
梦毁少年i 2020-11-28 05:06

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 posit

5条回答
  •  执念已碎
    2020-11-28 06:04

    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

提交回复
热议问题