欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C#条形码添加标题文字

程序员文章站 2022-07-14 17:56:27
...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;
using System.IO;


namespace Jbarcode
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //this.pictureBox1.BackColor = Color.Red;
            //ITF偶数码 MSI长度随意
            EncodingOptions options = new EncodingOptions
            {
                Width = 400,
                Height = 100 
            };
            BarcodeWriter writer = new BarcodeWriter
            {
                //Format = BarcodeFormat.MSI,
                Format = BarcodeFormat.CODE_39,
                Options = options
            };
            Bitmap bitmap = writer.Write("123456");
            Bitmap newbm = KiSetText(bitmap, "1234562222222222222222222222222", 10, 5);
            pictureBox1.Image = newbm;
        }


        public static Bitmap KiSetText(Bitmap b, string txt, int x, int y)
        {
            if (b == null)
            {
                return null;
            }

            Bitmap resizeImage = new Bitmap(b.Width,b.Height + 40);
            Graphics gfx = Graphics.FromImage(resizeImage);
            gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

            gfx.FillRectangle(Brushes.White, new Rectangle(0,0,400,100));

            gfx.DrawImageUnscaled(b, 0, 40);

            // 作为演示,我们用Arial字体,大小为32,红色。
            FontFamily fm = new FontFamily("Arial");
            Font font = new Font(fm, 24, FontStyle.Regular, GraphicsUnit.Pixel);
            SolidBrush sb = new SolidBrush(Color.Black);

            gfx.DrawString(txt, font, sb, new PointF(x, y));
            gfx.Dispose();

            return resizeImage;
        }


    }
}

生成效果:

C#条形码添加标题文字