ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

WinForm Code39条形码生成 + Chart图表(柱状图/饼图/数据库绑定)

2026/8/13 13:09:16 拓冰建站 浏览量
WinForm Code39条形码生成 + Chart图表(柱状图/饼图/数据库绑定)

一、Code39 条形码生成(原理 + 完整可运行代码)

1.1 Code39 核心原理(考试必背)

  • 黑白严格交替:条码竖单元永远是 黑→白→黑→白,不会连续同色

  • 单个字符固定9位:2宽单元 + 7窄单元,1=宽条,0=窄条

  • 完整结构:起始* + 分隔0 + 内容字符 + 分隔0 + 结束*

  • 支持字符集:数字0-9、大写字母A-Z、符号-. $/+%*

1.2 完整条形码生成代码

using System; using System.Drawing; using System.Windows.Forms; namespace _01条形码 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 生成标准Code39条形码位图 /// </summary> /// <param name="sourceCode">编码字符串</param> /// <returns>条形码Bitmap图片</returns> public Bitmap GetCode39(string sourceCode) { // 边距、宽窄条尺寸、条码高度配置 int leftMargin = 5; int topMargin = 0; int thickLength = 2; // 宽条宽度 int narrowLength = 1; // 窄条宽度 int barHeight = 35; int strLength = sourceCode.Length; // Code39起始/结束符 * 对应的二进制编码 string startEndCode = "010010100"; Font font = new Font("Segoe UI", 5); // Code39合法字符集(与编码数组一一对应) string alphaBet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*"; // Code39标准编码表(一一对应上面字符集) string[] code39EncodeArr = { "000110100", "100100001", "001100001", "101100000", "000110001", "100110000", "001110000", "000100101", "100100100", "001100100", "100001001", "001001001", "101001000", "000011001", "100011000", "001011000", "000001101", "100001100", "001001100", "000011100", "100000011", "001000011", "101000010", "000010011", "100010010", "001010010", "000000111", "100000110", "001000110", "000010110", "110000001", "011000001", "111000000", "010010001", "110010000", "011010000", "010000101", "110000100", "011000100", "010101000", "010100010", "010001010", "000101010", "010010100" }; // Code39只支持大写,统一转换 sourceCode = sourceCode.ToUpper(); string totalEncode = startEndCode; // 拼接每个字符编码,字符之间用0分隔 for (int i = 0; i < strLength; i++) { int index = alphaBet.IndexOf(sourceCode[i]); // 非法字符校验 if (index == -1) { Bitmap errBmp = new Bitmap(200, 50); Graphics gErr = Graphics.FromImage(errBmp); gErr.FillRectangle(Brushes.White, 0, 0, errBmp.Width, errBmp.Height); gErr.DrawString("存在不合法字符", SystemFonts.DefaultFont, Brushes.Red, 10, 10); return errBmp; } totalEncode += "0" + code39EncodeArr[index]; } // 拼接结束符 totalEncode += "0" + startEndCode; // 根据编码总长度创建位图 int totalWidth = 0; foreach (var c in totalEncode) { totalWidth += c == '1' ? thickLength : narrowLength; } Bitmap objBitMap = new Bitmap(totalWidth + leftMargin * 2, barHeight + topMargin * 2); Graphics g = Graphics.FromImage(objBitMap); g.FillRectangle(Brushes.White, 0, 0, objBitMap.Width, objBitMap.Height); // 逐位绘制黑白条码(奇偶交替配色) int currentX = leftMargin; for (int i = 0; i < totalEncode.Length; i++) { int barWidth = totalEncode[i] == '1' ? thickLength : narrowLength; Brush brush = i % 2 == 0 ? Brushes.Black : Brushes.White; g.FillRectangle(brush, currentX, topMargin, barWidth, barHeight); currentX += barWidth; } return objBitMap; } // 生成按钮点击事件 private void button1_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(textBox1.Text)) { pictureBox1.Image = GetCode39(textBox1.Text); } } } }

二、WinForm Chart 图表全套(柱状图静态数据 + 饼图数据库绑定)

2.1 辅助实体类

public class MyChart { public int X { get; set; } public int Y { get; set; } }

2.2 完整图表绘制代码

using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Drawing; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; namespace _03图表 { public partial class Form1 : Form { public Form1() { InitializeComponent(); Chart1(); // 静态柱状图 Chart2(); // 数据库饼图 } /// <summary> /// 静态数据柱状图(全套样式美化) /// </summary> public void Chart1() { // 1. 绑定XY轴静态数据 string[] xData = { "南山大队", "福田大队", "罗湖大队", "潮河大队" }; double[] yData = { 100, 150, 10, 97 }; chart1.Series[0].Points.DataBindXY(xData, yData); // 2. 多标题设置 chart1.Titles.Clear(); // 主标题 Title title1 = new Title("案情分析图"); title1.ForeColor = Color.Red; title1.Font = new Font("楷体", 12f, FontStyle.Italic); title1.Alignment = ContentAlignment.TopLeft; chart1.Titles.Add(title1); // 副标题 Title title2 = new Title("合计2026宗"); title2.ForeColor = Color.Black; title2.Font = new Font("楷体", 8f, FontStyle.Regular); title2.Alignment = ContentAlignment.TopRight; chart1.Titles.Add(title2); // 3. X轴样式配置 chart1.ChartAreas[0].AxisX.Interval = 1; chart1.ChartAreas[0].AxisX.IsInterlaced = true; chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -45; chart1.ChartAreas[0].AxisX.Title = "局所"; chart1.ChartAreas[0].AxisX.TitleFont = new Font("宋体", 20); chart1.ChartAreas[0].AxisX.TitleForeColor = Color.GreenYellow; chart1.ChartAreas[0].AxisX.LineColor = Color.Red; chart1.ChartAreas[0].AxisX.LabelStyle.Font = new Font("宋体", 10); chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = true; chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = ColorTranslator.FromHtml("#0000ff"); // 4. Y轴样式配置 chart1.ChartAreas[0].AxisY.LabelStyle.Angle = -45; chart1.ChartAreas[0].AxisY.Title = "宗数"; chart1.ChartAreas[0].AxisY.TitleFont = new Font("宋体", 20); chart1.ChartAreas[0].AxisY.TitleForeColor = Color.GreenYellow; chart1.ChartAreas[0].AxisY.LineColor = Color.Red; chart1.ChartAreas[0].AxisY.LabelStyle.Font = new Font("宋体", 10); // 5. 图例、数据标签 chart1.Series[0].LegendText = "案件数量"; chart1.Series[0].XValueType = ChartValueType.String; chart1.Series[0].Label = "#VAL"; // 显示数值 chart1.Series[0].LabelForeColor = Color.Red; } /// <summary> /// 数据库读取数据 + 饼图展示 /// </summary> public void Chart2() { List<MyChart> list = new List<MyChart>(); // 数据库连接字符串 string connStr = "server=.,1344;database=codefirstDB;uid=sa;password=123456"; using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); string sql = "select * from Table_1"; SqlCommand cmd = new SqlCommand(sql, conn); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { MyChart model = new MyChart(); model.X = Convert.ToInt32(reader["X"]); model.Y = Convert.ToInt32(reader["Y"]); list.Add(model); } } // 绑定数据、设置饼图样式 chart3.Series[0].Points.DataBind(list, "X", "Y", null); chart3.Series[0].ChartType = SeriesChartType.Pie; chart3.Series[0].ToolTip = "#VAL"; chart3.Titles.Clear(); chart3.Titles.Add("综合分析图"); } } }

三、核心知识点总结(考试重点)

1. Code39 条码核心

  • 交替绘图原理:i%2==0奇偶切换黑白条

  • 1=宽条、0=窄条,固定9位单字符编码

  • 首尾必须带 * 起始结束符,中间字符0分隔

2. Chart图表核心

  • 静态绑定:DataBindXY(x数组,y数组)

  • 实体绑定:DataBind(集合,X字段,Y字段)

  • 图表类型切换:SeriesChartType.Pie/Column

  • 内置变量:#VAL展示当前数值

  • 可自定义坐标轴颜色、角度、标题、网格线

3. 数据库饼图流程

打开连接 → 执行查询 → DataReader读取数据封装实体 → 集合绑定图表 → 渲染饼图