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都是咱們想要的效果,看起還算不錯,若是僅僅保存爲圖片還能夠,可是想要把這種佈局的圖片打印出來,結果是很不理想的。typescript


圖1佈局


圖2ui

圖1打印效果文字不夠平滑,很是難看,爲了讓圖片上的文字平滑,加上這麼一句話:g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;//反鋸齒,
加上這句話後顯示如圖2,圖2的效果貌似符合要求了,看着很是好,可是用二維碼打印機打印出的效果很是的不清晰,這下難住了,通過查不少資料得出結論:想要平滑的效果就必須犧牲清晰度。this

這樣的結論客戶確定不能接受,後來發現打印的事件提供了畫圖的Graphics的屬性改進後的代碼以下:spa

改進代碼

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); }
相關文章
相關標籤/搜索