對一維碼。以及二維碼生成進行配置生成,並且可以實現添加文字內容實現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; } } }
最后呈現結果