Search code examples
c#imageimage-processingitextbarcode

Display text underneath barcode when using code128.BarHeight


I am trying to display text underneath bar code but no luck so far.

This is what i have tried so far;

Bitmap bmpButton = new Bitmap(206, 38);
Bitmap bmpBackground = new Bitmap(CreateBarcode("AA12334566"));

// We use the default image as a background brush
TextureBrush objBrush = new TextureBrush(bmpBackground);

Graphics objGraphics;
objGraphics = Graphics.FromImage(bmpButton);
// objGraphics.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias

// We draw the background image...
objGraphics.FillRectangle(objBrush, 0, 0, 206, 38);

objGraphics.DrawString("AA1234567", new System.Drawing.Font("Arial", 8, FontStyle.Regular), SystemBrushes.WindowText, new Point(0, 14));

Response.ContentType = "image/jpeg";
bmpButton.Save(Response.OutputStream, ImageFormat.Jpeg);


private System.Drawing.Image CreateBarcode(string data)
{
Barcode128 code128 = new Barcode128();
code128.Code = data;
System.Drawing.Image barCode = code128.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White);
return barCode;
}

enter image description here


Solution

  • Code

            private void Form1_Load(object sender, EventArgs e)
            {
                Bitmap bmpButton = new Bitmap(206, 48);
                Graphics graphics = Graphics.FromImage(bmpButton);
    
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    
    
                graphics.FillRectangle(Brushes.White, new Rectangle(0, 0, 206, 48));
                graphics.DrawImage(CreateBarcode("AA12334566"), 5, 5);
                graphics.DrawString("AA12334566",new Font(FontFamily.GenericSansSerif, 12), Brushes.Black, new RectangleF(0, 28, 206, 20));
    
                graphics.Save();
                bmpButton.Save(@"D:\123.jpeg", ImageFormat.Jpeg);
            }
    
            private System.Drawing.Image CreateBarcode(string data)
            {
                Barcode128 code128 = new Barcode128();
                code128.Code = data;
                System.Drawing.Image barCode = code128.CreateDrawingImage(System.Drawing.Color.Black, System.Drawing.Color.White);
                return barCode;
            }
    

    Resulted image enter image description here

    AND PLEASE DO NOT STRETCH THE BAR CODE IMAGE AS IT WILL RESULT IN WRONG CONVERSIONS

    ////// Old Answer /////

    objGraphics.DrawString("AA1234567", new System.Drawing.Font("Arial", 8, FontStyle.Regular), SystemBrushes.WindowText, new Point(0, 14));
    

    in above line at the end [new Point(0, 14)] specifies the position of text as x-axis and y-axis and if you want to lower the position of text you can increase y instead of 14 use 28