Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
How can I save the barcode as an image?
Posted
Updated 3-Feb-14 19:15pm
v5
Comments
Marc Gabrie 3-Feb-14 10:38am    
that code does not generate any barcode! or are you using a barcode font? Elaborate/clarify a bit to get a better answer
[no name] 4-Feb-14 1:13am    
ya sorry i have used a barcode font and i want to save the barcode which is displayed as output and how could i do that??
Marc Gabrie 4-Feb-14 15:31pm    
have you tried "Solution 1" below?
[no name] 4-Feb-14 23:35pm    
no i did not understand it properly and i only need to convert my output which is in the form of barcode to image and save it into local folder..
Marc Gabrie 5-Feb-14 7:20am    
That code should work and is not complicated to understand. If you want to save the image on disk, then just using bitMap.Save() but specify a file path instead of a MemoryStream

1 solution

Please check the below mentioned links for more info.

protected void btnGenerate_Click(object sender, EventArgs e)
{
    string barCode = txtCode.Text;
    System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
    using (Bitmap bitMap = new Bitmap(barCode.Length * 40, 80))
    {
        using (Graphics graphics = Graphics.FromImage(bitMap))
        {
            Font oFont = new Font("IDAutomationHC39M", 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);
    }
}


Dynamically Generate and Display Barcode Image in ASP.Net

Generate barcodes using C#?


barcode generation in ASP.NET C# project
 
Share this answer
 

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