在 C# 中的图像上写入文本 [英] Write text on an image in C#

查看:17
本文介绍了在 C# 中的图像上写入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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(@"pathpicture.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.

推荐答案

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

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 = @"pathpicture.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

我添加加载和保存代码".

"I Add a load and save code".

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

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

这篇关于在 C# 中的图像上写入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆