Search code examples
c#winformsbarcode

add price to barcode generated serial


I have created a code to print barcode on a barcode printer, but now trying to add to the printing the price for the product which is in a textbox

here is my code

        private void frmAddProduct_Load(object sender, EventArgs e)
        {
            string barcode = txtCode.Text;

            Bitmap bitm = new Bitmap(barcode.Length * 40, 110);

            using (Graphics graphic = Graphics.FromImage(bitm))
            {


                Font newfont = new Font("IDAutomationHC39M", 14);
                PointF point = new PointF(2f, 2f);
                SolidBrush black = new SolidBrush(Color.Black);
                SolidBrush white = new SolidBrush(Color.White);
                graphic.FillRectangle(white, 0, 0, bitm.Width, bitm.Height);
                graphic.DrawString("*" + barcode + "*", newfont, black, point);


            }

            using (MemoryStream Mmst = new MemoryStream())
            {


                bitm.Save("ms", ImageFormat.Jpeg);
                pictureBox1.Image = bitm;
                pictureBox1.Width = bitm.Width;
                pictureBox1.Height = bitm.Height;


            }  
        }

        private void btnCodePrint_Click(object sender, EventArgs e)
        {
            short numCopies = 0;
            numCopies = Convert.ToInt16(txtCodeNo.Text);

            PrintDialog pd = new PrintDialog();

            pd.PrinterSettings = new PrinterSettings();

            if (DialogResult.OK == pd.ShowDialog(this))
            {

                PrintDocument pdoc = new PrintDocument();

                pdoc.PrintPage += new PrintPageEventHandler(pqr);
                pdoc.PrinterSettings.Copies = numCopies;

                pdoc.Print();

            }
        }

enter image description here

that's the issue I'm currently having, I want to add the price from the textbox into the barcode above or under the barcode in the image attached but I can't seem to find a way to make this work, everything I try makes the barcode not work

EDIT

I tried this but showed the price above the barcode

string barcode = txtCode.Text;
            string price = txtPprice.Text;
            Bitmap bitm = new Bitmap(barcode.Length * 40, 110);

            using (Graphics graphic = Graphics.FromImage(bitm))
            {


                Font newfont = new Font("IDAutomationHC39M", 14);
                Font newfont2 = new Font("Arial", 14);
                PointF point = new PointF(2f, 2f);
                SolidBrush black = new SolidBrush(Color.Black);
                SolidBrush white = new SolidBrush(Color.White);
                graphic.FillRectangle(white, 0, 0, bitm.Width, bitm.Height);
                graphic.DrawString("*" + price + "*", newfont2, black, point);
                graphic.DrawString("*" + barcode + "*", newfont, black, point);

Solution

  • This is your problem, point is actually the point where you are drawing the bar-code, and you are using it to draw your price as well. You will need to make a new point, i.e a different coordinate

    ...
    // a new point for your price
    PointF pointPrice = new PointF(20f, 20f);
    // Draw your price
    graphic.DrawString("*" + price + "*", newfont2, black, pointPrice);
    // Draw your barcode
    graphic.DrawString("*" + barcode + "*", newfont, black, point);
    

    Just play around with the values for pointPrice to figure out the placement position you want

    Good luck