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);
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM