https://www.2cto.com/database/201805/749421.html
https://zhidao.baidu.com/question/107832895.html
private
void
button2_Click(object sender, EventArgs e)
{
Bitmap image =
new
Bitmap(
200
,
200
);
Graphics g = Graphics.FromImage(image);
//使绘图质量最高,即消除锯齿
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality;
Pen p =
new
Pen(Color.Blue,
2
);
//定义了一个蓝色,宽度为的画笔
g.DrawString(
"字符串"
,
new
Font(
"宋体"
,
10
),
new
SolidBrush(Color.Blue),
new
PointF(
10
,
10
));
g.DrawLine(p,
0
,
0
,
200
,
200
);
//在画板上画直线
g.DrawRectangle(p,
0
,
0
,
200
,
200
);
//在画板上画矩形
g.DrawEllipse(p,
0
,
0
,
200
,
200
);
//在画板上画椭圆
//保存图片
image.Save(@
"c:1.jpg"
);
#region 显示
//pictureBox1.Image = (Image)image;
//pictureBox1.Image.Save(@"c:1.jpg");
#endregion
}
你在绘制字符串之前可以使用Graphics的MeasureString方法测量它的尺寸再绘制一个相应大小的矩形就行了,如下
Graphics g = this.CreateGraphics();
Font font = new Font("宋体", 9f);
PointF pointF = new PointF(10, 10);
SizeF sizeF = g.MeasureString("Hello World!", font);
g.FillRectangle(Brushes.White, new RectangleF(pointF, sizeF));
g.DrawString("Hello World!", font, Brushes.Black, pointF);
g.Dispose();
font.Dispose();
Graphics g = this.CreateGraphics();
Font font = new Font("宋体", 9f);
PointF pointF = new PointF(10, 10);
SizeF sizeF = g.MeasureString("Hello World!", font);
g.FillRectangle(Brushes.White, new RectangleF(pointF, sizeF));
g.DrawString("Hello World!", font, Brushes.Black, pointF);
g.Dispose();
font.Dispose();
//自己整合后写的,画正方形,中间写字
Bitmap image = new Bitmap(100, 50);
Graphics g = Graphics.FromImage(image);
Font font = new Font("黑体", 26.25f, FontStyle.Bold);
SizeF sizeF = g.MeasureString(lxtmStr, font);
//sizeF.Width, sizeF.Height
image = new Bitmap(Convert.ToInt32(sizeF.Width), Convert.ToInt32(sizeF.Height));
g = Graphics.FromImage(image);
//使绘图质量最高,即消除锯齿
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
PointF pointF = new PointF(0, 0);
if ((dsShoptemp.Tables[0].Rows[0]["STEP"].ObjToInt() % 2) == 1)
{
g.FillRectangle(Brushes.White, new RectangleF(pointF, sizeF));
g.DrawString(lxtmStr, font, Brushes.Black, pointF);
}
else
{
g.FillRectangle(Brushes.Black, new RectangleF(pointF, sizeF));
g.DrawString(lxtmStr, font, Brushes.White, pointF);
}
item["路线条码"] = image;
Graphics g = Graphics.FromImage(image);
Font font = new Font("黑体", 26.25f, FontStyle.Bold);
SizeF sizeF = g.MeasureString(lxtmStr, font);
//sizeF.Width, sizeF.Height
image = new Bitmap(Convert.ToInt32(sizeF.Width), Convert.ToInt32(sizeF.Height));
g = Graphics.FromImage(image);
//使绘图质量最高,即消除锯齿
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
PointF pointF = new PointF(0, 0);
if ((dsShoptemp.Tables[0].Rows[0]["STEP"].ObjToInt() % 2) == 1)
{
g.FillRectangle(Brushes.White, new RectangleF(pointF, sizeF));
g.DrawString(lxtmStr, font, Brushes.Black, pointF);
}
else
{
g.FillRectangle(Brushes.Black, new RectangleF(pointF, sizeF));
g.DrawString(lxtmStr, font, Brushes.White, pointF);
}
item["路线条码"] = image;