【发布时间】:2020-02-03 21:33:27
【问题描述】:
我正在尝试在位图上绘制文本,但图像变黑了。
protected Bitmap DrawTextImage(string text, float fontsize, string fontname = "Helvetica")
{
string imagePath = @"C:\img.bmp";
string imagePathTest = @"C:\imgTest.bmp";
Font textFont = new Font(fontname, fontsize);
var size = TextRenderer.MeasureText(text, textFont);
Bitmap bmp = new Bitmap(size.Width, size.Height);
Graphics graphics = Graphics.FromImage(bmp);
SolidBrush brush = new SolidBrush(Color.Black);
graphics.DrawString(text, textFont, brush, size.Width, size.Height);
if(File.Exists(imagePathTest))
File.Delete(imagePathTest);
bmp.Save(imagePathTest, ImageFormat.Bmp);
对于它的价值,图像最终还需要转换为位图格式,以便在热敏打印机上打印,但我暂时只关注这部分。
我在这里使用的参数是DrawTextImage(text, 36);
【问题讨论】:
-
@maccettura
graphics.DrawString()将修改底层图像 -
@maccettura 。
Graphics对象不包含任何图形;它是一个工具,可让您绘制到相关位图上,包括控件的表面。 - 不过,它应该在using子句中创建。 -
您正在用黑色画笔在黑色位图旁边绘制。
-
您仍在位图右侧绘制。在 (0,0) 处绘制!
-
它已经是已发布答案的一部分。只要语言警察在巡逻,我就不再在这里发布答案了。
标签: c# image-processing bitmap