c# zxing生成二维码和打印

摘要:
生成二维码代码asset=“要生成的字符串”;publicstaticBitmapCreateQRCode(stringasset){EncodingOptionsoptions=newQrCodeEncodingOptions{DisableECI=true,CharacterSet="UTF-8",Width=120,Height=120};BarcodeWriterwriter=newBar

生成二维码代码

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都是我们想要的效果,看起还算不错,如果仅仅保存为图片还可以,但是想要把这种布局的图片打印出来,结果是很不理想的。

c# zxing生成二维码和打印第1张
图1

c# zxing生成二维码和打印第2张
图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);
    }

免责声明:文章转载自《c# zxing生成二维码和打印》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Oracle触发器详解yum仓库本地搭建下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

python tkinter 学生信息管理系统

使用tkinter模块,python3.6,主要功能有添加,查询,删除,修改学生信息 使用模版: 1 from tkinter import * 2 import tkinter.font as tkFont 3 import tkinter as tk 4 from tkinter import ttk 最主要也是最难做的是,实现不同功能的界面在同一TK...

超级强大的SVG SMIL animation动画详解

//zxx: 本文的SVG在有缓存时候是无动画效果,此时您可以试着【右键-新标签页打开图片】。 一、SVG SMIL animation概览 1. SMIL是什么?SMIL不是指「水蜜梨」,而是Synchronized Multimedia Integration Language(同步多媒体集成语言)的首字母缩写简称,是有标准的。本文所要介绍的SVG动画...

Docker中使用Dockerfile定制化jar启动时:at sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1264)

场景 CentOS7中使用Dockerfile部署后台jar包: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/119818808 在上面使用Dockfile部署带后台jar包的镜像时提示: at sun.awt.FontConfiguration.getVersion(FontConf...

C#红绿状态灯

1.在Label里 画圆,存在窗体刷新会丢失画。 public void SetShowConnectStatus(Label lbl, boolisOk) { lbl.Text = ""; Graphics gra =lbl.CreateGraphics();...

h5固定表头公共样式

    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />    <meta content="yes" name="apple-mo...

第二章 JSP页面制作基础

第二章  JSP页面制作基础 [本章导读] JSP页面将Java代码嵌入到HTML脚本中,掌握HTML语言是学习JSP的基础。HTML是网页制作的一种规范,一种标准,它通过标记符来标记网页的各个部分。本章首先介绍用HTML制作网页的各种标记符的设置方法,接着介绍了CSS的基本概念和使用方法。此外,为了增强读者对网页制作的了解,介绍了Dreamweaver的...