【发布时间】:2018-07-03 09:28:57
【问题描述】:
我正在尝试生成一个包含一些文本的 BMP 文件。我创建了一个winform应用程序,我可以成功创建BMP(我将它显示在图片框上没有问题)。但是,当我将其保存到文件时,我只得到了一张黑色图像。
我的代码是
private void btnNameUsage_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(width, height);
string name = "Hello how are you";
string date = DateTime.Now.Date.ToString();
Graphics thegraphics = Graphics.FromImage(bmp);
string complete = date+"\n"+name ;
using (Font font1 = new Font("Arial", 24, FontStyle.Regular, GraphicsUnit.Pixel))
using (var sf = new StringFormat()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center,
})
{
thegraphics.DrawString(complete, font1, Brushes.Black, new Rectangle(0, 0, bmp.Width, bmp.Height), sf);
}
picBoxImage.Image = bmp; //THIS WORKS
//thegraphics.Flush();//I am not sure this is necessary and it changes nothing anyway
bmp.Save(@"theImage.bmp",ImageFormat.Bmp);//I tried only one argument but it gave a png file. Now only a black BMP
}
我迷失了我在这里做错了什么。感谢帮助。
【问题讨论】:
-
您正在用黑色画笔绘制文本。你期待什么背景?如果它是透明的,则适用于 PNG。 BMP 是另一回事,因此您应该检查是否将黑色文本保存在黑色背景上。使用不同的文本颜色尝试一次。
-
先为您的文本尝试不同的画笔怎么样?这更容易。
-
@j03p 检查了您的链接。如果您看到代码,您将看到我已经在执行该链接解决方案并且无法正常工作。不是重复的
-
@KansaiRobot - 请参阅 stackoverflow.com/questions/12502365/… 并根据您的需要调整大小。
-
您应该将您的图形对象放在 using(...) {...} 块中,以正确清理其锁定的内存资源。
标签: c# bitmap image-manipulation bmp