I have a simple Windows Form Application where I am trying to convert some text into an image. The application starts by loading all the fonts into a combobox.
The user then selects which font they'd like to use from the combobox item list [font] then it changes the font in a label control to match the selected font. After, it takes the text in the label and converts the text into an image and displays the image in a picturebox (code below).
It works perfectly except for when I select my font "code 128", which is the font I need. I had to download the font ["code 128"] and install it on my computer so that I could create some barcodes. When I select "code 128" as my desired font, the text will appear in the label control but noting will show in the picturebox. It's the only font that will not work.
The reason I am doing this is because my label printer will not recognize my code 128 font but I can print images. I have another function [not displayed here] that calculates the checksum and adds the beginning and end points to the barcode which is working perfectly.
Using c# on windows 10.
If I'm missing any details or not clear, please let me know.
private void Form1_Load(object sender, EventArgs e)
{
//Add all font names on computer to combobox1
foreach (FontFamily fonts in System.Drawing.FontFamily.Families)
{
comboBox1.Items.Add(fonts.Name);
}
}
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
//textBox1 Text = "TEST"
//once the combobox1 selection is changed, relay the selected item's font to label1 and copy textBox1.Text to the label.
label1.Font = new Font(comboBox1.Text, 20);
label1.Text = textBox1.Text;
//convert the textBox1.Text to an image and place it in the pictureBox1.
pictureBox1.Image = convertText2Image();
}
private Image convertText2Image()
{
//this is how we convert the text to an image.
Bitmap bmp = new Bitmap(1, 1);
Graphics graphics = Graphics.FromImage(bmp);
Font f = label1.Font; //use the exact font as label1, which is selected from the comboBox.
SizeF sf = graphics.MeasureString(label1.Text, f);
bmp = new Bitmap(bmp, (int)sf.Width, (int)sf.Height);
graphics = Graphics.FromImage(bmp);
graphics.DrawString(label1.Text, f, Brushes.Black, 0, 0);
f.Dispose();
graphics.Flush();
graphics.Dispose();
return bmp;
}