C#生成二维码及打印

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/black_yu/article/details/79392235
//生成二维码图片代码
    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);
       // Font font = new Font("宋体", 12);
        //Brush bru = Brushes.Blue; 
        //for (int i = 1; i <= 5; i++)
        //{
         //   e.Graphics.DrawString("Hello world ", font, bru, i*20, i*20);
        //}
    }
展开阅读全文

没有更多推荐了,返回首页