Click here to Skip to main content
15,888,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to print the BarCode based on user input successfully, but the BarCode is not filling correctly. Image Height and Width is increasing/decreasing as user inputs.

Please see the below reference image.

Imgur: The magic of the Internet[^]

Please suggest filling the barcode based on user inputs.

What I have tried:

For these, I tried below code.


C#
public void fun()
    {
        string barCode = txtCode.Text;
        System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
        using (Bitmap bitMap = new Bitmap(Convert.ToInt32(txtW.Text), Convert.ToInt32(txtH.Text)))
        {
            using (Graphics graphics = Graphics.FromImage(bitMap))
            {
                Font oFont = new Font(@"C:\Users\bojjaiah.thoti\Downloads\IDAutomationCode39\IDAutomation.com Free Code 39 Font\IDAutomationHC39M.ttf", 16);
                PointF point = new PointF(2f, 2f);
                SolidBrush blackBrush = new SolidBrush(Color.Black);
                SolidBrush whiteBrush = new SolidBrush(Color.White);
                graphics.FillRectangle(whiteBrush, 0, 0, bitMap.Width, bitMap.Height);
                graphics.DrawString("*" + barCode + "*", oFont, blackBrush, point);
            }
            using (MemoryStream ms = new MemoryStream())
            {
                bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                byte[] byteImage = ms.ToArray();

                Convert.ToBase64String(byteImage);
                imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);

            }

            plBarCode.Controls.Add(imgBarCode);
        }
    }
Posted
Updated 6-Mar-19 22:41pm

1 solution

You have set the height and width of your bitmap, and the starting position of the text, but you never set the height and width of the font that you are using.
 
Share this answer
 
Comments
Bojjaiah 7-Mar-19 7:01am    
Font height is read-only property, right?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900