对一维码。以及二维码生成进行配置生成,并且可以实现添加文字内容实现x,y轴定位坐标位置
1.以下方法是生成一维码,二维码,文字的通用方法
/// <summary> /// 添加字体方法 /// </summary> /// <param name="g">Graphics g ,目标Graphics对象 </param> /// <param name="drawPoint">PointF drawPoint,存储坐标位置</param> /// <param name="data">string data ,准备添加的字符串</param> /// <param name="size">大小</param> private void AddFont(Graphics g, PointF drawPoint, string data, int size) { SolidBrush mybrush = new SolidBrush(Color.Black); //设置默认画刷颜色 Font myfont = new Font("宋体", size, FontStyle.Regular); //设置默认字体格式 g.DrawString(data, myfont, mybrush, drawPoint); //图片上添加文字 //刷新pictureBox调用此方法:pictureBox1.Refresh(); } /// <summary> /// 生成二维码 引用:using ThoughtWorks.QRCode.Codec; /// </summary> /// <param name="g">Graphics g ,目标Graphics对象 </param> /// <param name="drawPoint">PointF drawPoint,存储坐标位置</param> /// <param name="data">string data ,准备添加的字符串</param> /// <param name="width">二维码宽度</param> /// <param name="height">二维码高度</param> private void CreateQRCode(Graphics g, PointF drawPoint, string data, int width, int height) { QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE; qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M; qrCodeEncoder.QRCodeScale = 3; //作为初始定义,没有实际意义 qrCodeEncoder.QRCodeVersion = 7; //二维码生成的类型, //Encoding.UTF8为必备参数,否则某些中文字符无法识别 System.Drawing.Image image = qrCodeEncoder.Encode(data, Encoding.UTF8); #region 根据设定的目标图片尺寸调整二维码QRCodeScale设置 if (width > 0) { //当设定目标图片尺寸大于生成的尺寸时,逐步增大方格尺寸 #region 当设定目标图片尺寸大于生成的尺寸时,逐步增大方格尺寸 while (image.Width < width) { qrCodeEncoder.QRCodeScale++; System.Drawing.Image imageNew = qrCodeEncoder.Encode(data, Encoding.UTF8); if (imageNew.Width < width) { image = new System.Drawing.Bitmap(imageNew); imageNew.Dispose(); imageNew = null; } else { qrCodeEncoder.QRCodeScale--; //新尺寸未采用,恢复最终使用的尺寸 imageNew.Dispose(); imageNew = null; break; } } #endregion //当设定目标图片尺寸小于生成的尺寸时,逐步减小方格尺寸 #region 当设定目标图片尺寸小于生成的尺寸时,逐步减小方格尺寸 while (image.Width > width && qrCodeEncoder.QRCodeScale > 1) { qrCodeEncoder.QRCodeScale--; System.Drawing.Image imageNew = qrCodeEncoder.Encode(data, Encoding.UTF8); image = new System.Drawing.Bitmap(imageNew); imageNew.Dispose(); imageNew = null; if (image.Width < width) { break; } } #endregion } #endregion g.DrawImage(image, drawPoint); } /// <summary> /// 生成一维码 引用:using BarcodeLib /// </summary> /// <param name="g">Graphics g ,目标Graphics对象 </param> /// <param name="drawPoint">PointF drawPoint,存储坐标位置</param> /// <param name="data">string data ,准备添加的字符串</param> /// <param name="width">二维码宽度</param> /// <param name="height">二维码高度</param> private void CreateBrCode(Graphics g, PointF drawPoint, string data, int width, int height) { var barcode = new Barcode(); barcode.IncludeLabel = false; //是否显示下方数字 barcode.Alignment = AlignmentPositions.CENTER; //居中显示 barcode.Width = width; //宽度 barcode.Height = height; //高度 barcode.RotateFlipType = RotateFlipType.RotateNoneFlipNone;//无旋转 barcode.BackColor = Color.White; //背景颜色 barcode.ForeColor = Color.Black; //条形码颜色 Image barcodeImg = barcode.Encode(TYPE.CODE128B, data); //将字符进行转化Image g.DrawImage(barcodeImg, drawPoint); }
2.在这里通过winform的按钮事件去调用,也可以根据实际操作去调用
/// <summary> /// 生成按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(txt_QrCode.Text) && string.IsNullOrWhiteSpace(txt_Font1_Content.Text) && string.IsNullOrWhiteSpace(txt_Font2_Content.Text) && string.IsNullOrWhiteSpace(txt_Font3_Content.Text)) { MessageBox.Show("条码内容以及文字内容必须填写一项"); return; } if (!string.IsNullOrWhiteSpace(txt_QrCode.Text)) { if (string.IsNullOrWhiteSpace(txt_QrCodeX.Text) || string.IsNullOrWhiteSpace(txt_QrCodeY.Text) || string.IsNullOrWhiteSpace(txt_QrCodeWidth.Text) || string.IsNullOrWhiteSpace(txt_QrCodeHeight.Text)) { MessageBox.Show("条码内容不为空,请填写完整的条码信息内容"); return; } if (radioButton2.Checked) { if (txt_QrCodeWidth.Text != txt_QrCodeHeight.Text) { MessageBox.Show("二维码的宽度,高度应该保持一致"); return; } } } if (!string.IsNullOrWhiteSpace(txt_Font1_Content.Text)) { if (string.IsNullOrWhiteSpace(txt_Font1_X.Text) || string.IsNullOrWhiteSpace(txt_Font1_Y.Text) || string.IsNullOrWhiteSpace(txt_Font1_Size.Text)) { MessageBox.Show("文字1内容不为空,请填写完整的文字1信息内容"); return; } } if (!string.IsNullOrWhiteSpace(txt_Font2_Content.Text)) { if (string.IsNullOrWhiteSpace(txt_Font2_X.Text) || string.IsNullOrWhiteSpace(txt_Font2_Y.Text) || string.IsNullOrWhiteSpace(txt_Font2_Size.Text)) { MessageBox.Show("文字2内容不为空,请填写完整的文字2信息内容"); return; } } if (!string.IsNullOrWhiteSpace(txt_Font3_Content.Text)) { if (string.IsNullOrWhiteSpace(txt_Font3_X.Text) || string.IsNullOrWhiteSpace(txt_Font3_Y.Text) || string.IsNullOrWhiteSpace(txt_Font3_Size.Text)) { MessageBox.Show("文字3内容不为空,请填写完整的文字3信息内容"); return; } } Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height); //创建 Graphics 对象,将pictureBox1作为背景图像进行内容的背景图像 Graphics graphics = Graphics.FromImage(bitmap); ////添加一个白色画布 //Rectangle Rec = new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height); //SolidBrush mySolidBrush = new SolidBrush(Color.White); //graphics.FillRectangle(mySolidBrush, Rec); //当条码内容部分数据不为空,界面显示条码 if (!string.IsNullOrWhiteSpace(txt_QrCode.Text)) { PointF DrawPoint = new PointF(Convert.ToInt32(txt_QrCodeX.Text), Convert.ToInt32(txt_QrCodeY.Text)); //判断是否是一维码或者是二维码 if (radioButton2.Checked) CreateQRCode(graphics, DrawPoint, txt_QrCode.Text, Convert.ToInt32(txt_QrCodeWidth.Text), Convert.ToInt32(txt_QrCodeHeight.Text)); else if (radioButton1.Checked) CreateBrCode(graphics, DrawPoint, txt_QrCode.Text, Convert.ToInt32(txt_QrCodeWidth.Text), Convert.ToInt32(txt_QrCodeHeight.Text)); } //当文字1内容部分数据不为空,界面显示内容1的数据 if (!string.IsNullOrWhiteSpace(txt_Font1_Content.Text)) { //创建 PointF 有序列类存储 float x,float y的数据(内容的坐标轴位置) PointF DrawPoint = new PointF(Convert.ToInt32(txt_Font1_X.Text), Convert.ToInt32(txt_Font1_Y.Text)); AddFont(graphics, DrawPoint, txt_Font1_Content.Text, Convert.ToInt32(txt_Font1_Size.Text)); } //当文字2内容部分数据不为空,界面显示内容1的数据 if (!string.IsNullOrWhiteSpace(txt_Font2_Content.Text)) { PointF DrawPoint = new PointF(Convert.ToInt32(txt_Font2_X.Text), Convert.ToInt32(txt_Font2_Y.Text)); AddFont(graphics, DrawPoint, txt_Font2_Content.Text, Convert.ToInt32(txt_Font2_Size.Text)); } //当文字3内容部分数据不为空,界面显示内容1的数据 if (!string.IsNullOrWhiteSpace(txt_Font3_Content.Text)) { PointF DrawPoint = new PointF(Convert.ToInt32(txt_Font3_X.Text), Convert.ToInt32(txt_Font3_Y.Text)); AddFont(graphics, DrawPoint, txt_Font3_Content.Text, Convert.ToInt32(txt_Font3_Size.Text)); } //刷新pictureBox1 pictureBox1.Image = bitmap; }
3.在这里通过winform的按钮事件去清空界面,通过遍历窗体的内部控件元素,进行删除
/// <summary> /// 清空姐界面内容 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { //遍历整个窗体的元素 foreach (Control item in this.Controls) { //如果是文本框 if (item is TextBox) { item.Text = null; } if (item is PictureBox) { //方法一: ((PictureBox)item).Image = null; ////方法二: ////清空picture内容 //Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height); //Graphics graphics = Graphics.FromImage(bitmap); ////添加一个透明画布 //Rectangle Rec = new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height); //SolidBrush mySolidBrush = new SolidBrush(Color.Transparent); //graphics.FillRectangle(mySolidBrush, Rec); //((PictureBox)item).Image = bitmap; } } }
最后呈现结果