在我的程序中,我允许用户输入一些文本,然后使用 graphics.DrawString()
方法将这些文本放在图像的顶部。当我然后去保存这张图片时,它保存时没有文本。
如何将两者保存为一张图片?
我看过几个例子,但没有一个有帮助。
private void txtToolStripMenuItem_Click(object sender, System.EventArgs e)
{
Rectangle r = new Rectangle(535, 50, original_image.Width, original_image.Height);
Image img = Image.FromFile("C:\\PCB.bmp");
Bitmap image = new Bitmap(img);
StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Center;
strFormat.LineAlignment = StringAlignment.Center;
Graphics g = Graphics.FromImage(image);
g.DrawString("Hellooooo", new Font("Tahoma", 40), Brushes.White,
r, strFormat);
image.Save("file_PCB.Bmp", ImageFormat.Bmp);
}
最佳答案
那是因为您正在创建一个没有 Canvas 的图形对象。您没有绘制任何内容,因此绘制文本不会改变任何内容。
首先创建图像的副本(或创建空白位图并在其上绘制图像),然后创建用于在该图像上绘制的图形对象:
Graphics g = Graphics.FromImage(image_save);
然后绘制文字并保存图像。
关于c# - 将文字添加到图像并保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13892308/