Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 粗体生成的条形码中字符串的一部分_C#_Winforms_Visual Studio_Barcode - Fatal编程技术网

C# 粗体生成的条形码中字符串的一部分

C# 粗体生成的条形码中字符串的一部分,c#,winforms,visual-studio,barcode,C#,Winforms,Visual Studio,Barcode,我正在生成一个条形码作为图像。条形码由几个不同的值组成,如数量、长度、宽度和平方米。这些数字显示在条形码下方,作为所有用户条目的摘要。有没有办法在条形码下方的摘要中加粗或加下划线的m2(平方米)?请参见以下所需内容示例: 以下是我用来生成条形码的代码: private void generate_Click(object sender, EventArgs e) { String barcode = summary.Text;

我正在生成一个条形码作为图像。条形码由几个不同的值组成,如数量、长度、宽度和平方米。这些数字显示在条形码下方,作为所有用户条目的摘要。有没有办法在条形码下方的摘要中加粗或加下划线的m2(平方米)?请参见以下所需内容示例:

以下是我用来生成条形码的代码:

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;
            }
        }

您可以使用接受字体样式()

问题在于确定文本的哪一部分应该加粗,这意味着您必须在某个点将文本拆分,并确定加粗文本的偏移量

由于您使用的字体()也会呈现条形码,因此除非您找到一种不同的字体,允许您将条形码单独呈现到文本中,否则这将不起作用。这就给你留下了一些选择

  • 分开的字体
  • 不要将要加粗的文本设为粗体
  • 以不同的方式使其突出,用不同的颜色使文本突出/在下面划线/等等
  • 如果这只是文字 你需要把课文分成两部分

    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);
    
    然后分3个阶段渲染文本,测量每个前一部分的偏移量:

    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);
    
    但是,字体包括行 所以我认为你能做的最好的事情就是用同样的测线技术画一条下划线:

    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;
    

    旁注:
    Bitmap
    Font
    SolidBrush
    都实现了
    IDisposable
    ,因此您可能应该使用
    语句将它们包装在
    中。不,这是不可能的。字母是条形码字体的一部分,不能与条形分开加粗。但您始终可以使用粗体(urgh)是的,确切地说,但是添加FontStyle。粗体也会使条形码本身加粗,导致其无效且无法被扫描仪读取。@Truex如果您只希望部分文本加粗,则必须将其从主绘图部分拆分出来,即另一个
    g.DrawString
    请参阅上面添加的示例,我添加了FontStyle.Bold,它使条形码也变得更厚font@weston-哦,对了,我明白了,是一种呈现条形图的字体。。。Truex,我想你可能需要使用一种不同的字体来分别呈现这些条。哦,我明白了,这种字体确实包括线条!抱歉@Truex,我没有发现你是OP,也不知道你在说什么
    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;