Write text on an image in C#

后端 未结 5 1213
梦毁少年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 05:45

    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();
    

提交回复
热议问题