0

I have an array of type PictureBox. I want to fill it the List of string and then covert it to the Barcode. But I am uncle to convert the string to the PictureBox. Is there any step I can do to make them compatible?

System.Windows.Forms.PictureBox[] PictureBoxArray = new PictureBox[3];
List<string> serial = new List<string>;

public void ConvertToBarCode()
{
   BarcodeLib.TYPE barcodetype1 = BarcodeLib.TYPE.CODE39;
   BarcodeLib.Barcode bar1 = new BarcodeLib.Barcode();
   bar1.IncludeLabel = true;
   PictureBoxArray[0] = serial[0]; // Want to Convert String to PictureBox
   PictureBoxArray[0].Image = bar1.Encode(barcodetype1, SerialNumberList[0]);
}

I have filles the serial List with the string now just want the conversion.

Nilesh Gajare
  • 6,302
  • 3
  • 42
  • 73
Nosheen Javed
  • 175
  • 1
  • 5
  • 21
  • What do you expect the line to do, even if it compiled? what do you expect to happen when you convert a string to a picturebox? – nvoigt Jan 21 '14 at 06:16
  • you can look may question it could help http://stackoverflow.com/questions/20244595/convert-a-string-to-a-bitmap-in-c-sharp – LFS96 Jan 21 '14 at 06:17
  • I first have to fill the PictureBox array then change its Image property. – Nosheen Javed Jan 21 '14 at 06:18
  • we know but how look the sting? – LFS96 Jan 21 '14 at 06:21
  • My string looks like this "S1101243" now I use the Encode method to take the string and pass it to the Image property of the PictureBox like this PictureBoxArray[0].Image = bar1.Encode(barcodetype1, S1101243); But before that I have to pass value to the PictureBox[0] otherwise it will give the NullReferenceException.. And my actual problem is this http://stackoverflow.com/questions/21228198/how-to-print-the-barcode-format-in-c-sharp – Nosheen Javed Jan 21 '14 at 06:27
  • ok your logic have an mistake you cant create an pic with a such littet sting but te string is the identyfyer to find the picture ore let it calculate see the other question – LFS96 Jan 21 '14 at 06:59

1 Answers1

1

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

Deepak Sharma
  • 4,124
  • 1
  • 14
  • 31
  • 2
    Obviously the OP already has a barcode library. Your solution might work, but is far away from the original code, does not use the barcode library any more and was exchanged by a new library (which is the font in this case). – Thomas Weller Jan 21 '14 at 10:25
  • @ThomasW. has posted this same question 2-3 place and I have asked him. He want me to use this free 3 of 9 font. its the explanation how can we create an image from two string. if you want to use another lib its upto you. you just need to modify 2-3 line in above code. MIND IT. – Deepak Sharma Jan 21 '14 at 10:38
  • Deepak: Thank you very much for this class. I'm using it to display the barcode representation of part ID numbers and I was having trouble getting the image into a PictureBox using font 39. Incidentally, I was already using the "Free 3 of 9" download for other projects and it works perfectly; I don't see any issues with using it as others suggested. BTW, I suggest you not use ImageLayout.Stretch on the BackgroundImageLayout property and instead go with 'Center' and a larger font size. Stretching creates problems with scanners trying to read a possibly blurred image. Thanks again. --Tim – Tim Bostwick Oct 01 '14 at 04:20