2017-01-16 45 views
0

我试图在C#中使用GDI绘制Code 128条形码。我有.ttf字体这应该工作作为矢量图形(未RASTR):GDI绘制条形码文本

 var bmp = new Bitmap(900, 100); 
     var g = Graphics.FromImage(bmp); 
     g.Clear(Color.White); 
     g.SmoothingMode = SmoothingMode.None; 
     g.InterpolationMode = InterpolationMode.HighQualityBicubic; 
     g.PixelOffsetMode = PixelOffsetMode.HighQuality; 
     g.DrawString(value, new Font("Code 128", 72), Brushes.Black, 0, 50); 
     g.DrawPath(new Pen(Brushes.Black, (float) 1.0), new GraphicsPath()); 
     g.Flush(); 

结果图像是宽的,我想将其缩放到0.5(仅x轴)。但是当我将X缩放到450时, - 图像变得不清晰。我注意到,图像(当它还是900)轻微模糊: enter image description here

我想如果图像看起来就像在这里我broblem将迎刃而解:

enter image description here

如何以这种方式绘制字符串?

+0

Switch InterpolationMode也是关闭的!结果必须符合最近的像素。 – TaW

+0

谢谢@taw我试过了,但结果是一样的 – burzhuy

+0

这些已经为我工作:'e.Graphics.SmoothingMode = SmoothingMode.None; e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit; e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;' – TaW

回答

2

平滑文本由TextRenderingHint属性控制。只需在渲染字符串之前设置此属性:

g.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;