You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C# WinForm生成的条码中添加产品价格

在C# WinForm条码打印中添加产品价格到条码上下的解决方案

我来帮你搞定这个问题!你遇到的核心问题是价格和条码位置重叠,再加上原Bitmap高度不够,才会出现位置不对或者条码失效的情况。咱们一步步来修正:

先理清问题根源

  1. 你之前错误地给价格也加了*前缀后缀,这是Code 39条码的专属标识,价格完全不需要
  2. 价格和条码用了同一个坐标点,必然会重叠在一起
  3. 原Bitmap高度只有110,放不下额外的价格文本,空间不够导致显示异常

修改后的完整代码

private void frmAddProduct_Load(object sender, EventArgs e)
{
    string barcode = txtCode.Text;
    string price = txtPprice.Text;
    // 调整Bitmap高度,留出足够空间容纳价格和条码(原110改成180)
    Bitmap bitm = new Bitmap(barcode.Length * 40, 180);

    using (Graphics graphic = Graphics.FromImage(bitm))
    {
        Font barcodeFont = new Font("IDAutomationHC39M", 14);
        Font priceFont = new Font("Arial", 12); // 价格用普通字体,字号可以调整
        SolidBrush blackBrush = new SolidBrush(Color.Black);
        SolidBrush whiteBrush = new SolidBrush(Color.White);

        // 先填充白色背景
        graphic.FillRectangle(whiteBrush, 0, 0, bitm.Width, bitm.Height);

        // --- 1. 打印价格(示例放在条码上方,居中显示)---
        // 计算价格文本的居中位置
        SizeF priceSize = graphic.MeasureString(price, priceFont);
        float priceX = (bitm.Width - priceSize.Width) / 2;
        float priceY = 5; // 价格的Y坐标,靠近顶部
        graphic.DrawString(price, priceFont, blackBrush, priceX, priceY);

        // --- 2. 打印条码(居中显示,和价格拉开距离)---
        string barcodeText = $"*{barcode}*"; // 条码必须保留前后*,这是Code39的要求
        SizeF barcodeSize = graphic.MeasureString(barcodeText, barcodeFont);
        float barcodeX = (bitm.Width - barcodeSize.Width) / 2;
        float barcodeY = 50; // 条码的Y坐标,和价格拉开足够空间
        graphic.DrawString(barcodeText, barcodeFont, blackBrush, barcodeX, barcodeY);
    }

    using (MemoryStream Mmst = new MemoryStream())
    {
        bitm.Save(Mmst, ImageFormat.Jpeg);
        pictureBox1.Image = bitm;
        pictureBox1.Width = bitm.Width;
        pictureBox1.Height = bitm.Height;
    }
}

private void btnCodePrint_Click(object sender, EventArgs e)
{
    short numCopies = 0;
    if (short.TryParse(txtCodeNo.Text, out numCopies) == false)
    {
        MessageBox.Show("请输入有效的打印份数");
        return;
    }

    PrintDialog pd = new PrintDialog();
    pd.PrinterSettings = new PrinterSettings();
    if (DialogResult.OK == pd.ShowDialog(this))
    {
        PrintDocument pdoc = new PrintDocument();
        pdoc.PrintPage += new PrintPageEventHandler(pqr);
        pdoc.PrinterSettings.Copies = numCopies;
        pdoc.Print();
    }
}

// 确保打印事件方法pqr能正确绘制生成的图片
private void pqr(object sender, PrintPageEventArgs e)
{
    if (pictureBox1.Image != null)
    {
        // 打印时居中显示条码和价格
        float x = (e.PageBounds.Width - pictureBox1.Image.Width) / 2;
        float y = (e.PageBounds.Height - pictureBox1.Image.Height) / 2;
        e.Graphics.DrawImage(pictureBox1.Image, x, y);
    }
}

关键修改说明

  1. 调整Bitmap高度:把原高度110改成180,确保能同时放下价格和条码
  2. 分开设置坐标:给价格和条码分配不同的Y轴坐标,彻底解决重叠问题;如果想把价格放在条码下方,只需调换priceY和barcodeY的数值即可
  3. 文本居中处理:通过MeasureString计算文本宽度,让价格和条码都居中显示,更美观专业
  4. 修正条码格式:只给条码保留*前缀后缀,价格完全不需要,确保条码能被正常识别
  5. 增加输入校验:打印份数部分加了TryParse,避免无效输入导致报错

这样修改后,价格会清晰显示在条码上方(或下方),条码也能正常识别和打印啦!

内容的提问来源于stack exchange,提问作者Joey Arnanzo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:49:21