1. 程式人生 > >c# zxing生成二維碼和列印

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

相關推薦

c# zxing生成列印

生成二維碼程式碼 asset=“要生成的字串”; public static Bitmap CreateQRCode(string asset) { EncodingOptions options = new QrCodeEncodingOptions {

zxing生成條碼

/*** * 生成二維碼方法 * @param str 生成內容 * @param widthHeight 寬度和高度 * @return * @throws WriterException */ public static Bit

Zxing 生成條形碼去掉白邊

原始碼下載: 需求:根據輸入內容,生成條形碼或者二維碼。 我們大多數會選擇Zxing。因為jar包較小。且使用簡單。根據內容生成二維碼的工具類也是一搜一大堆。上面的原始碼裡面也提供了一個。但是我們仔細看了下。會發現。不管生成的是條形碼還是二維碼都會有一部分的白邊。如圖

Zxing生成新增Logo

1.一個專案開始,都需要做準備,準備一個Zxing。jar包來支撐生成二維碼   接下來直接上程式碼,   生成普通二維碼    先寫一個生成二維碼的方法 //生成二維碼 private B

【java】google的zxing架包生成讀取【可帶文字logo】

oms cga dispose framework 增加 span 記錄 ora obj 承接RC4生成不重復字符串的需求之後,因為優惠碼要方便用戶使用的緣故,所以思來想去,覺得還是直接生成二維碼給用戶直接掃比較實用,也不用用戶專門記錄冗長的優惠碼編號。 =========

ZXing-core生成解析

現在二維碼這麼流行的時刻,也必須知道二維碼是怎麼生成。現在我們就來看看,是怎麼生成的。 其實主要是利用goggle釋出的jar來使用:本文轉自點選開啟連結 1、二維碼的生成    二維碼的生成需要藉助MatrixToImageWriter類,該類是由Google提供的

java 使用zxing生成(帶logo文字說明的)

jar包maven地址        <dependency>             <groupId>com.google.zxing</groupId>             <artifactId>core<

zxing生成

fault awt .com void auth args dom systems import <dependency> <groupId>com.google.zxing</groupId> <artifact

Android:使用ZXing生成(支持加入Logo圖案)

over rmi api note sta size argb_8888 reat for循環 ZXing是谷歌的一個開源庫。能夠用來生成二維碼、掃描二維碼。本文所介紹的是第一部分。 首先上效果圖: ZXing相關各種文件官方下載地址:https://g

zxing生成設置邊框顏色

嘗試 edi osi 循環 span static right 開始 top 真是研究了很久很久,滿滿的淚啊 zxing生成二維碼,默認是可以增加空白邊框的,但是並沒有說設置邊框顏色的屬性。 其中增加空白邊框的屬性的一句話是: Map hints = new Ha

WPF調用zxing生成

大小 pac xaml returns pri 進行 writer 創建 idt 1.登錄http://zxingnet.codeplex.com/,下載對應.net版本的zxing庫 2.引入zxing.dll 3.新建界面控件 using System; using

java學習-zxing生成矩陣的簡單例子

map obj 基於 The output 圖片 .get imageio sts 這個例子需要使用google的開源項目zxing的核心jar包 core-3.2.0.jar 可以百度搜索下載jar文件 也可使用maven添加依賴 <de

zxing生成以流式傳到頁面

@RequestMapping(“/makeQrCode”) public void madeQrCode(HttpServletResponse res,String content,Integer width,Integer height) throws IOException{

Zxing 生成

首先新增依賴 implementation ‘cn.bingoogolapple:bga-qrcode-zxing:1.2.5’ 新增許可權 佈局 MainActivity package com.lll.zxingsc; import android.Manifes

使用zxing生成工具類

public class QRCodeUtils { /** * 建立二維碼(有白邊) * * @param string * @return */ public static Bitmap c

SpringBoot 使用 zxing 生成 返回Base64編碼

前置知識 生成二維碼目前的技術目前有兩大類:QRCode 和 Zxing QRCode 是日本原生的二維碼生成技術,目前只有 0.5Beta 版且不能通過maven等構件工具引入 Zxing 是google 對二維碼生成技術的包裝,提供給Android系統用,不過也可以

生成 掃描

導依賴 implementation 'cn.bingoogolapple:bga-qrcode-zxing:1.3.4' 許可權 <uses-permission android:name="android.permission.CAMERA" /> <uses-pe

Android平臺利用Zxing生成與解析圖片中的

轉載請註明http://blog.csdn.net/houkai6/article/details/47102733 1. 生成二維碼 public final class EncodingHandler { private static final int BLACK

使用google的zxing生成

二維碼,又叫二維條碼。 1.java這邊的話生成二維碼有很多開發的jar包如 zxing,qrcode ,下面是使用zxing的開發包來實現的。 ZXing專案是google code上面提供的一個關於條碼編解碼的開源專案。  2.先下載zxing開發包,這裡用到的只是co

解決ZXING生成圖片白框太大的問題

最近專案裡需要生成一些二維碼,使用之後發現一些問題,生成之後的圖片,白色邊框區域太大了,導致二維碼內容區域太小。 百度了一下,有人說設定EncodeHintType.MARGIN屬性即可,這個屬性值為1-4,實際測試發現並沒有什麼卵用。(順便說一下,一些比較老的版本中,這個