在我的程序中,我允许用户输入一些文本,然后使用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);
}
那是因为您正在创建一个没有画布的图形对象。您没有绘制任何内容,因此绘制文本不会改变任何内容。
首先创建图像的副本(或创建一个空白位图并在其上绘制图像),然后创建一个用于在该图像上绘制的图形对象:
Graphics g = Graphics.FromImage(image_save);
然后绘制文本并保存图像。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)