Bold a part of string in generated barcode

2019-07-19 09:46发布

I am generating a barcode as an image. The barcode consists of few different values, such as amount, length, width and m2. These numbers are displaying below the barcode as a summary of all user entries. Is there a way to either bold or underline the m2 (square meters) in the summary under the barcode? Please see below sample of what is needed:

Sample outcome

Here's the code I use to generate the barcode:

private void generate_Click(object sender, EventArgs e)
        {

            String barcode = summary.Text;
            Bitmap bitmap = new Bitmap(barcode.Length * 40, 150);
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
            Font ofont = new System.Drawing.Font("IDAutomationHC39M", 20);
            PointF point = new PointF (2f, 2f);
                SolidBrush black = new SolidBrush(Color.Black);
                SolidBrush White = new SolidBrush(Color.White);
                graphics.FillRectangle(White, 0, 0, bitmap.Width, bitmap.Height);
                graphics.DrawString("*" + barcode + "*", ofont, black, point);
            }
            using (MemoryStream ms = new MemoryStream())
            {
                bitmap.Save(ms, ImageFormat.Png);
                box4.Image = bitmap;
                box4.Height = bitmap.Height;
                box4.Width = bitmap.Width;
            }
        }

enter image description here

2条回答
萌系小妹纸
2楼-- · 2019-07-19 10:26

You can use the constructor of Font that accepts a font style (docs)

new System.Drawing.Font("IDAutomationHC39M", 20, FontStyle.Bold);

The problem comes in determining what part of the text should be bold which means you will have to split the text up at a certain point, and ascertain the offset of the bold text

Since the font you're using (IDAutomationHC39M), renders the bar code also, this won't work unless you find a different font that will allow you to render the bars separately to the text. This leaves you with a few options.

  1. Separate fonts
  2. Don't make the text you want to bold
  3. Make it stand out in a different way, colour the text in a different colour that will make it stand out / draw a line under it / etc
查看更多
forever°为你锁心
3楼-- · 2019-07-19 10:26

If this was just text

You need to break the text up into 2 parts,

string barcode1; //the normal bit
string barcode2; //the bold/underlined bit

Font ofont = new System.Drawing.Font("IDAutomationHC39M", 20);
Font ofontBold = new System.Drawing.Font("IDAutomationHC39M", 20, FontStyle.Bold);

Then render text in 3 stages, measuring the offset of each previous part:

graphics.DrawString("*" + barcode1, ofont, black, point);

var point2 = new PointF(point.X + graphics.MeasureString("*" + barcode1, ofont).Width, point.Y);
graphics.DrawString(barcode2, ofontBold, black, point2);

var point3 = new PointF(point2.X + graphics.MeasureString(barcode2, ofontBold).Width, point2.Y);
graphics.DrawString("*", ofont, black, point3);

However the font includes the lines

So I think the best you can do is to draw an underline using the same string measuring techniques:

string barcode1; //the normal bit
string barcode2; //the underlined bit

var lineStartX = point.X + graphics.MeasureString("*" + barcode1, ofont).Width;
var lineWidth = graphics.MeasureString(barcode2).Width;
查看更多
登录 后发表回答