CSharp生成二维条码的步骤

一、Demo和控件下载

打开http://www.FzBozc.com/CSharp.html下载 客户端演示DEMO到本地,并进行解压打开。

DEMO采用VS2010开发,需要FrameWork4.0开发。

CSharp生成二维条码的步骤_第1张图片

二、类库引用

将下载的类库COPY到开发目录下,点击VS下的“引用”右键,选择“添加引用”,将类库引用到程序中。

CSharp生成二维条码的步骤_第2张图片
三、编码参数设置

根据分页参数设置,将设置的参数写入对应条码参数对象中。

private EncodingOptions GetOption(int nType = 0)

        {

 EncodingOptions options;

            if(nType == 0)    //PDF417

            {

                FzBozc.PDF417.PDF417EncodingOptions pdf =

                    newFzBozc.PDF417.PDF417EncodingOptions();

                pdf.Aspect =DDL_Aspect.SelectedValue;

                pdf.Compact =Chk_Compact.Checked;

                pdf.Cols =ToInt(TB_Cols.Text);

                pdf.Rows = ToInt(TB_Rows.Text);

                pdf.XScale =ToInt(DDL_XScale.SelectedValue);

                pdf.YScale =ToInt(DDL_YScale.SelectedValue);

                pdf.ErrorCorrection =(FzBozc.PDF417.Internal.PDF417ErrorLevel)ToInt(DDL_EccPDF.SelectedValue);

                options = pdf;

            }

            elseif (nType == 1)           //Qr_Code

            {

                FzBozc.QrCode.QrCodeEncodingOptions qr =new FzBozc.QrCode.QrCodeEncodingOptions();

                qr.MdScale =ToInt(DDL_Scale.SelectedValue);

                if(DDL_EccQR.SelectedValue =="L")qr.ErrorCorrection = FzBozc.QrCode.Internal.QrErrorLevel.L;

                elseif (DDL_EccQR.SelectedValue =="M") qr.ErrorCorrection =FzBozc.QrCode.Internal.QrErrorLevel.M;

                elseif (DDL_EccQR.SelectedValue =="H") qr.ErrorCorrection =FzBozc.QrCode.Internal.QrErrorLevel.H;

                elseqr.ErrorCorrection = FzBozc.QrCode.Internal.QrErrorLevel.Q;

                options = qr;

            }

            elseif (nType == 2)    //DataMatrix

            {

                FzBozc.Datamatrix.DatamatrixEncodingOptions dm =new FzBozc.Datamatrix.DatamatrixEncodingOptions();

                dm.MdScale =ToInt(DDL_Scale.SelectedValue);

                options = dm;

            }

            else        //Aztec

            {

                FzBozc.Aztec.AztecEncodingOptions azt =new FzBozc.Aztec.AztecEncodingOptions();

                azt.MdScale =ToInt(DDL_Scale.SelectedValue);

                azt.ErrorCorrection =ToInt(TB_EccPercent.Text);

                options = azt;

            }

            options.Width =ToInt(TB_Width.Text);

            options.Height = ToInt(TB_Height.Text);

            return(EncodingOptions)options;

四. 生成条码

根据参数设置和条码内容,生成条码。

条码需要保存到服务端对应的目录下,如“BarImage”目录

网站目录要具有写入权限。

protectedvoid Btn_Create_Click(object sender,EventArgse)

  {

           try

            {

               intk = DDL_BarType.SelectedIndex;

               BarcodeFormatfmtBarCode = BarcodeFormat.PDF_417;

               if(k == 1) fmtBarCode = BarcodeFormat.QR_CODE;

               if(k == 2) fmtBarCode = BarcodeFormat.DATA_MATRIX;

                if(k == 3) fmtBarCode =BarcodeFormat.AZTEC;

               EncodingOptionsBarOptions = GetOption(k);

 

               varwriter = new BarcodeWriter

                {

                    Format = fmtBarCode,

                    Options = BarOptions

                };

                System.Drawing.Image img= writer.Write(TB_Info.Text);

 

               //输出图像

               stringfilename = Guid.NewGuid().ToString() +".jpg";

               stringfilepath =Server.MapPath(@"~\BarImage")+"\\" + filename;

                System.IO.FileStream fs =newSystem.IO.FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);

                img.Save(fs,System.Drawing.Imaging.ImageFormat.Jpeg);

                fs.Close();

               

                pictBar.ImageUrl ="BarImage/"+ filename;               

                pictBar.Width = img.Width;

                pictBar.Height = img.Height;

                img.Dispose();

            }

           catch(Exception ex )

            {

                Response.Write("");

            }

        }


若有开发问题咨询 QQ:409487486 (纯技术交流,非诚勿扰)

你可能感兴趣的:(CSharp生成二维码)