用 C# 在图像上写入文本

2024-05-08

我有以下问题。我想在位图图像中制作一些图形,例如债券形式

我可以在图像中写入文字
但我会在不同的位置写更多的文字

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

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

如何写入文本并保存,并在保存的图像中写入另一个文本。


要绘制多个字符串,请调用graphics.DrawString多次。您可以指定绘制字符串的位置。在这个例子中,我们将绘制两个字符串“Hello”,“Word”(“Hello”为蓝色,“Word”为红色):

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:“我添加加载和保存代码”。

您可以随时打开位图文件Image.FromFile,并使用上面的代码在其上绘制一个新文本。然后保存图像文件bitmap.Save

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

用 C# 在图像上写入文本 的相关文章

随机推荐