How to Add the values of List into the List after encoding it to Barcode in c#

后端 未结 3 2077
傲寒
傲寒 2020-12-12 06:07

I have two List one is of string and the other is of PictureBox type. I want to take the values of List of type string and the convert it into the Barcode then save it to th

3条回答
  •  青春惊慌失措
    2020-12-12 06:53

    enter image description here

    you want like this.. right?? see this is the representaion of the this string "S1253551" in 3of9 and plain text and finally as image right??

        public Image stringToImage(string inputString)
        { 
            string text = inputString.Trim();
    
            Bitmap bmp = new Bitmap(1, 1);
    
            //Set the font style of output image
            Font font = new Font("Free 3 of 9", 25, FontStyle.Regular, GraphicsUnit.Pixel);
            Font font2 = new Font("Arial", 15, FontStyle.Regular, GraphicsUnit.Pixel);
    
            Graphics graphics = Graphics.FromImage(bmp);
    
            int width = (int)graphics.MeasureString(text, font).Width;
            int height = (int)graphics.MeasureString(text, font).Height;
    
            int height2 = (int)graphics.MeasureString(text, font2).Height;
    
            bmp = new Bitmap(bmp, new Size(width, height+height2));
            graphics = Graphics.FromImage(bmp);
    
    
    
            //Specify the background color of the image
            graphics.Clear(Color.Cyan);
            graphics.SmoothingMode = SmoothingMode.AntiAlias;
            graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
    
    
    
            //Specify the text, font, Text Color, X position and Y position of the image
            graphics.DrawString(text, font, new SolidBrush(Color.Black), 0, 0);
            graphics.DrawString(text, font2, new SolidBrush(Color.Black), 0, height);
    
            graphics.Flush();
            graphics.Dispose();
    
            //if you want to save the image  uncomment the below line.
            //bmp.Save(@"d:\myimage.jpg", ImageFormat.Jpeg);
    
            return bmp;
        }
    

    Remember you must have installed "free 3 of 9" font.

    you pass the string "S1253551" and it generate the barcode and add the plain text at bottom and finally return it as image.

    Its working code i have tried at my end. Enjoy. :)

    Download the working code from here Download

提交回复
热议问题