C# 生成二维码

QrCode.net地址 http://qrcodenet.codeplex.com

QrCode.net 是用C#编写生成二维码的项目,它支持C#、silverlight、winRT

 

新建一个winform程序后,引用Gma.QrCodeNet.Encoding.dll

然后在工具箱中添加一个选项卡,然后右键选择项,选择Gma.QrCodeNet.Encoding.dll,然后出现如图所示的控件

将两个控件拖入winform窗体中

 

private Color _lightModule = Color.FromArgb(220, 100, 203, 50);
        private Color _darkModule = Color.FromArgb(170, 20, 250, 220);

        public FrmQr()
        {
            InitializeComponent();
            qcicHello.DarkBrush = new SolidBrush(_darkModule);
            qcicHello.LightBrush = new SolidBrush(_lightModule);
            qcicHello.Text = txtInput.Text.Trim();
            qcgcHello.Text = txtInput.Text.Trim();
            ContrastCal();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = @"PNG (*.png)|*.png|Bitmap (*.bmp)|*.bmp|Encapsuled PostScript (*.eps)|*.eps|SVG (*.svg)|*.svg";
            saveFileDialog.FileName = Path.GetFileName(GetFileNameProposal());
            saveFileDialog.DefaultExt = "png";

            if (saveFileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            if (saveFileDialog.FileName.EndsWith("eps"))
            {
                BitMatrix matrix = qcgcHello.GetQrMatrix();

                // 初始化 the EPS renderer
                var renderer = new EncapsulatedPostScriptRenderer(
                    new FixedModuleSize(6, QuietZoneModules.Two), // Modules size is 6/72th inch (72 points = 1 inch)
                    new FormColor(Color.Black), new FormColor(Color.White));

                using (var file = File.Open(saveFileDialog.FileName, FileMode.CreateNew))
                {
                    renderer.WriteToStream(matrix, file);
                }
            }
            else if (saveFileDialog.FileName.EndsWith("svg"))
            {
                BitMatrix matrix = qcgcHello.GetQrMatrix();

                // 初始化 EPS renderer
                var renderer = new SVGRenderer(
                    new FixedModuleSize(6, QuietZoneModules.Two), // Modules size is 6/72th inch (72 points = 1 inch)
                    new FormColor(Color.FromArgb(150, 200, 200, 210)), new FormColor(Color.FromArgb(200, 255, 155, 0)));

                using (var file = File.OpenWrite(saveFileDialog.FileName))
                {
                    renderer.WriteToStream(matrix, file, false);
                }
            }
            else
            {

                GraphicsRenderer gRender = new GraphicsRenderer(new FixedModuleSize(30, QuietZoneModules.Four));
                BitMatrix matrix = qcgcHello.GetQrMatrix();
                using (FileStream stream = new FileStream(saveFileDialog.FileName, FileMode.Create))
                {
                    gRender.WriteToStream(matrix, ImageFormat.Png, stream, new Point(600, 600));
                }
            }
        }

        private void txtInput_TextChanged(object sender, EventArgs e)
        {
            qcicHello.Text = txtInput.Text.Trim();
            qcgcHello.Text = txtInput.Text.Trim();
        }

        private string GetFileNameProposal()
        {
            return txtInput.Text.Length > 10 ? txtInput.Text.Substring(0, 10) : txtInput.Text;
        }


        private void ContrastCal()
        {
            SolidBrush darkmoduleBrush = qcicHello.DarkBrush as SolidBrush;
            SolidBrush lightmoduleBrush = qcicHello.LightBrush as SolidBrush;
            Color darkmodule = darkmoduleBrush == null ? _darkModule : darkmoduleBrush.Color;
            Color lightmodule = lightmoduleBrush == null ? _lightModule : lightmoduleBrush.Color;

            Contrast ctrast = ColorContrast.GetContrast(new FormColor(lightmodule), new FormColor(darkmodule));

            label1.Text = ctrast.Ratio.ToString();
        }
    }
View Code

 

posted on 2014-09-02 16:24  lovezj9012  阅读(925)  评论(0)    收藏  举报

导航