天天看點

c#生成一維碼,二維碼。文字,可以設定尺寸大小,以及顯示位置

對一維碼。以及二維碼生成進行配置生成,并且可以實作添加文字内容實作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;
                }
            }
        }      

最後呈現結果