zoukankan      html  css  js  c++  java
  • c# zxing生成二维码和打印

    生成二维码代码

    asset=“要生成的字符串”;
    public static Bitmap CreateQRCode(string asset)
        {
            EncodingOptions options = new QrCodeEncodingOptions
            {
                DisableECI = true,
                CharacterSet = "UTF-8",
                Width = 120,
                Height = 120
            };
            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            writer.Options = options;
            return writer.Write(asset);
        }
    

    生成图片的方法###

    public static Image GetPrintPicture(Bitmap image, AssetEntity asset, int picWidth, int picHeight)
        {
            Bitmap printPicture = new Bitmap(picWidth, picHeight);
            int height = 5;
            Font font = new Font("黑体", 10f);
            Graphics g = Graphics.FromImage(printPicture);
            Brush brush = new SolidBrush(Color.Black);
    
            g.SmoothingMode = SmoothingMode.HighQuality;
    
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;//如果不填加反锯齿代码效果如图1
    
            int interval = 15;
            int pointX = 5;
            Rectangle destRect = new Rectangle(190, 10, image.Width, image.Height);
            g.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
            height += 8;
            RectangleF layoutRectangle = new RectangleF(pointX, height, 260f, 85f);
            g.DrawString("资产编号:" + asset.Serial, font, brush, layoutRectangle);
    
            height += interval;
            layoutRectangle = new RectangleF(pointX, height, 230f, 85f);
            g.DrawString("资产名称:" + asset.Name, font, brush, layoutRectangle);
    
            height += interval;
            layoutRectangle = new RectangleF(pointX, height, 230f, 85f);
            g.DrawString("类    别:" + asset.Category, font, brush, layoutRectangle);
    
            height += interval;
            layoutRectangle = new RectangleF(pointX, height, 230f, 85f);
            g.DrawString("规格型号:" + asset.Model, font, brush, layoutRectangle);
    
            height += interval;
            layoutRectangle = new RectangleF(pointX, height, 230f, 85f);
            g.DrawString("生产厂家:" + asset.Manufacturer, font, brush, layoutRectangle);
    
    
            height += interval;
            layoutRectangle = new RectangleF(pointX, height, 230f, 85f);
            g.DrawString("启用时间:" + asset.StartUseTime, font, brush, layoutRectangle);
    
            height += interval;
            layoutRectangle = new RectangleF(pointX, height, 230f, 85f);
            g.DrawString("资产价格:" + asset.Price, font, brush, layoutRectangle);
    
            height += interval;
            layoutRectangle = new RectangleF(pointX, height, 230f, 85f);
            g.DrawString("保管单位:" + asset.Department, font, brush, layoutRectangle);
    
            //height += interval;
            layoutRectangle = new RectangleF(pointX + 150, height, 230f, 85f);
            g.DrawString("保管人:" + asset.Manager, font, brush, layoutRectangle);
    
            height += interval;
            layoutRectangle = new RectangleF(pointX, height, 230f, 85f);
            g.DrawString("存放地点:" + asset.StoragePlace, font, brush, layoutRectangle);
    
            height += interval;
            layoutRectangle = new RectangleF(pointX, height, 240f, 85f);
            g.DrawString("备    注:" + asset.Remark, font, brush, layoutRectangle);
    
            return printPicture;
        }
    

    显示效果如下###

    图1与图2都是我们想要的效果,看起还算不错,如果仅仅保存为图片还可以,但是想要把这种布局的图片打印出来,结果是很不理想的。


    图1


    图2

    图1打印效果文字不够平滑,非常难看,为了让图片上的文字平滑,加上这么一句话:g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;//反锯齿,
    加上这句话后显示如图2,图2的效果貌似符合要求了,看着非常好,但是用二维码打印机打印出的效果非常的不清晰,这下难住了,经过查很多资料得出结论:想要平滑的效果就必须牺牲清晰度。

    这样的结论客户肯定不能接受,后来发现打印的事件提供了画图的Graphics的属性改进后的代码如下:

    改进代码###

     	public static void GetPrintPicture(Bitmap image, AssetEntity asset, PrintPageEventArgs g)
            {
                int height = 5;
                Font font = new Font("宋体", 10f);
                Brush brush = new SolidBrush(Color.Black);
                g.Graphics.SmoothingMode = SmoothingMode.HighQuality;
                int interval = 15;
                int pointX = 5;
                Rectangle destRect = new Rectangle(190, 10, image.Width, image.Height);
                g.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
                height += 8;
                RectangleF layoutRectangle = new RectangleF(pointX, height, 260f, 85f);
                g.Graphics.DrawString("资产编号:" + asset.Serial, font, brush, layoutRectangle);
    
                height += interval;
                layoutRectangle = new RectangleF(pointX, height, 230f, 85f);
                g.Graphics.DrawString("资产名称:" + asset.Name, font, brush, layoutRectangle);
    
                height += interval;
                layoutRectangle = new RectangleF(pointX, height, 230f, 85f);
                g.Graphics.DrawString("类    别:" + asset.Category, font, brush, layoutRectangle);
    
                height += interval;
                layoutRectangle = new RectangleF(pointX, height, 230f, 85f);
                g.Graphics.DrawString("规格型号:" + asset.Model, font, brush, layoutRectangle);
    
                height += interval;
                layoutRectangle = new RectangleF(pointX, height, 230f, 85f);
                g.Graphics.DrawString("生产厂家:" + asset.Manufacturer, font, brush, layoutRectangle);
    
    
                height += interval;
                layoutRectangle = new RectangleF(pointX, height, 230f, 85f);
                g.Graphics.DrawString("启用时间:" + asset.StartUseTime, font, brush, layoutRectangle);
    
                height += interval;
                layoutRectangle = new RectangleF(pointX, height, 230f, 85f);
                g.Graphics.DrawString("资产价格:" + asset.Price, font, brush, layoutRectangle);
    
                height += interval;
                layoutRectangle = new RectangleF(pointX, height, 230f, 85f);
                g.Graphics.DrawString("保管单位:" + asset.Department, font, brush, layoutRectangle);
    
                //height += interval;
                layoutRectangle = new RectangleF(pointX + 150, height, 230f, 85f);
                g.Graphics.DrawString("保管人:" + asset.Manager, font, brush, layoutRectangle);
    
                height += interval;
                layoutRectangle = new RectangleF(pointX, height, 230f, 85f);
                g.Graphics.DrawString("存放地点:" + asset.StoragePlace, font, brush, layoutRectangle);
    
                height += interval;
                layoutRectangle = new RectangleF(pointX, height, 240f, 85f);
                g.Graphics.DrawString("备    注:" + asset.Remark, font, brush, layoutRectangle);
    
            }
    

    打印代码###

     	private void sbtnPrintQRCode_Click(object sender, EventArgs e)
            {
                PrintDocument document = new PrintDocument();
                Margins margins = new Margins(0x87, 20, 5, 20);
                document.DefaultPageSettings.Margins = margins;
                PrintPreviewDialog dialog = new PrintPreviewDialog();
                document.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
                dialog.Document = document;
                try
                {
                    if (this.CurrentPrintQRCode != null && this.CurrentPrintQRCode.Count() > 0)
                    {
                       document.Print();
                       Thread.Sleep(1000);
                    }
                }
                catch (Exception exception)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show(exception.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    document.PrintController.OnEndPrint(document, new PrintEventArgs());
                }
            }
    
    private void pd_PrintPage(object sender, PrintPageEventArgs e) //触发打印事件
        {
            //PointF f = new PointF(20, 10);//原来方法
            //if (currentPrintImage != null)
            //{
            //    e.Graphics.DrawImage(this.currentPrintImage, f);
            //}
            CreatePicture.GetPrintPicture(this.bitmap, this.assetInfo, e);
        }
  • 相关阅读:
    5G网络逐渐普及TSINGSEE青犀视频云边端架构网页视频实时互动直播系统又将如何发展?
    【开发记录】TSINGSEE青犀视频云边端架构Visual Studio 2017自建WebRTC中peerconnection_client编译报无法解析错误
    安防视频云服务平台EasyCVR视频智能分析系统运行控制台报404错误如何排查?
    一对一或一对多音视频通话会议系统可以通过哪些方式实现?
    TSINGSEE青犀视频云边端视频智能分析平台开发VMware下安装Ubuntu系统后无法安装VMwaretools问题解决
    最简单的Windows套接字(Socket)例子(源码,实例)
    KJAVA虚拟机Hack笔记实现MIDP的SLAVE事件模型
    系统程序员成长计划你的数据放在哪里(下)
    使用new实现realloc操作
    KJava虚拟机hack笔记基于GTK的移植
  • 原文地址:https://www.cnblogs.com/zwgblogs/p/4232024.html
Copyright © 2011-2022 走看看