Search code examples
c#drawingbarcodegdisystem.drawing

GDI draw barcode text


I'm trying to use GDI in C# to draw Code 128 barcode. I have .ttffont which should work as vector graphic (not rastr):

        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();

the result image is wide, i want to scale it to 0.5 (x axis only). But when i scale X to 450,- image becomes not clear. I noticed that image (when it still 900) is lightly blurred: enter image description here

i think my broblem will be solved if image will look like here:

enter image description here

How to draw string in such manner?


Solution

  • Smoothing of text is controlled by the TextRenderingHint property. Just set this property prior to rendering your string:

    g.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;