-1

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

user1263981
  • 2,953
  • 8
  • 57
  • 98
  • you can increase 14, instead of 14 use 28, new Point(0, 28) – Muhammad Waqas Aziz Oct 10 '20 at 04:16
  • Yes, always remember with computer graphics that the origin 0,0 is the top left and bigger values of y are further down the screen. This is probably unlike every graph you ever drew in math class; one of those things you just remember and get used to – Caius Jard Oct 10 '20 at 08:10
  • i have tried changing 14 to 28 and higher number but text just disappears. – user1263981 Oct 10 '20 at 11:01

1 Answers1

1

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