Exemplo n.º 1
0
        /// <summary>
        /// 产生当前实体条码、二维码图片
        /// </summary>
        /// <returns></returns>
        public Bitmap GetCodeBitmap()
        {
            #region 变量
            string strText       = string.Empty;
            string elementType   = string.Empty;   //元素类型
            string codeType      = string.Empty;   //条码类型
            string strCode       = string.Empty;   //要生成的条码、二维码内容
            int    width         = 0;              //元素宽度
            int    height        = 0;              //元素高度
            int    x             = 0;              //元素x位置
            int    y             = 0;              //元素y位置
            string fontFamilyStr = string.Empty;   //字体

            string imgPath         = string.Empty; //图片路径
            string xmlEntityFields = string.Empty; //xml中配置的字段

            Bitmap bitMapCode = null;

            #endregion
            try
            {
                #region 生成条码、二维码
                C_CodeInfo codeInfo = codePrintCodfig.PrintElements.CodeInfo;
                if (codeInfo != null)
                {
                    #region CodeType
                    codeType = codeInfo.CodeType;
                    #endregion

                    #region EntityFields
                    strCode = AnalyseXmlEntityFields(codeInfo.EntityFields.Trim());
                    if (codeInfo.IsEncrip)
                    {
                        strCode = AESHelper.Encrypt(strCode);
                    }
                    #endregion

                    #region ElementPosition
                    x = codeInfo.ElementPosition.X;
                    y = codeInfo.ElementPosition.Y;
                    #endregion

                    #region ElementStyle
                    width  = codeInfo.ElementStyle.Width;
                    height = codeInfo.ElementStyle.Height;
                    #endregion

                    bitMapCode = new Bitmap(width, height);


                    if (codeType.ToUpper().Trim() != "QR_CODE")
                    {
                        bitMapCode = CreateCode(strCode, width, height, GetBarcodeFormat(codeType));
                    }
                    else
                    {
                        bitMapCode = CreateQRCode(strCode, width, height);
                    }
                }

                #endregion
                return(bitMapCode);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                printDocument.Dispose();
                // bitMapCode.Dispose();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// PrintDocument事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void PrintEvent(object sender, PrintPageEventArgs e)
        {
            #region 变量
            string strText         = string.Empty;
            string elementType     = string.Empty; //元素类型
            string codeType        = string.Empty; //条码类型
            string strCode         = string.Empty; //要生成的条码、二维码内容
            int    width           = 0;            //元素宽度
            int    height          = 0;            //元素高度
            int    x               = 0;            //元素x位置
            int    y               = 0;            //元素y位置
            string fontFamilyStr   = string.Empty; //字体
            int    fontSize        = 0;            //字体Size
            int    fontStyle       = 0;            //参考枚举FontStyle
            string imgPath         = string.Empty; //图片路径
            int    linePointStartX = 0;            //线条开始点x
            int    linePointStartY = 0;            //线条开始点y
            int    linePointEndX   = 0;            //线条结束点x
            int    linePointEndY   = 0;            //线条结束点y
            int    lineWidth       = 0;            //线条宽度
            string xmlEntityFields = string.Empty; //xml中配置的字段
            Font   font;

            #endregion

            #region 根据XML配置信息在打印纸上绘图

            using (Graphics gr = e.Graphics)
            {
                #region 绘制条码、二维码
                C_CodeInfo codeInfo = codePrintCodfig.PrintElements.CodeInfo;
                if (codeInfo != null)
                {
                    #region CodeType
                    codeType = codeInfo.CodeType;
                    #endregion

                    #region EntityFields
                    strCode = AnalyseXmlEntityFields(codeInfo.EntityFields.Trim());
                    if (codeInfo.IsEncrip)
                    {
                        strCode = AESHelper.Encrypt(strCode);
                    }
                    #endregion

                    if (!string.IsNullOrEmpty(strCode))
                    {
                        #region ElementPosition
                        x = codeInfo.ElementPosition.X;
                        y = codeInfo.ElementPosition.Y;
                        #endregion

                        #region ElementStyle
                        width  = codeInfo.ElementStyle.Width;
                        height = codeInfo.ElementStyle.Height;
                        #endregion

                        if (codeType.ToUpper().Trim() != "QR_CODE")
                        {
                            using (Image bitMapCode = CreateCode(strCode, width, height, GetBarcodeFormat(codeType)))
                            {
                                gr.DrawImage(bitMapCode, x, y, width, height);
                            }
                        }
                        else
                        {
                            using (Image bitMapCode = CreateQRCode(strCode, width, height))
                            {
                                gr.DrawImage(bitMapCode, x, y, width, height);
                            }
                        }
                    }
                }
                #endregion

                #region 绘制其他元素
                List <PrintElement> printElements = codePrintCodfig.PrintElements.OtherInfos;
                foreach (PrintElement pe in printElements)
                {
                    #region ElementType
                    elementType = pe.ElementType.Trim();
                    #endregion

                    #region EntityFields
                    strText = AnalyseXmlEntityFields(pe.EntityFields.Trim());
                    #endregion

                    #region ElementPosition
                    x = pe.ElementPosition.X;
                    y = pe.ElementPosition.Y;
                    #endregion

                    #region ElementStyle
                    width         = pe.ElementStyle.Width;
                    height        = pe.ElementStyle.Height;
                    fontFamilyStr = pe.ElementStyle.FontFamilyStr;
                    fontSize      = pe.ElementStyle.FontSize;
                    fontStyle     = pe.ElementStyle.FontStyle;
                    imgPath       = pe.ElementStyle.ImgPath;


                    #endregion

                    switch (elementType.ToLower())
                    {
                    case "img":
                        FileStream fs           = new FileStream(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\CodePrintImg\" + imgPath, FileMode.OpenOrCreate);
                        Bitmap     img          = new Bitmap(fs);
                        Rectangle  imgRectangle = new Rectangle(x, y, width, height);
                        gr.DrawImage(img, imgRectangle);
                        fs.Dispose();
                        break;

                    case "line":
                        linePointStartX = pe.ElementPosition.X;
                        linePointStartY = pe.ElementPosition.Y;
                        linePointEndX   = pe.ElementPosition.EndX;
                        linePointEndY   = pe.ElementPosition.EndY;
                        lineWidth       = pe.ElementStyle.LineWidth;
                        Point linePointStart = new Point(linePointStartX, linePointStartY);
                        Point linePointEnd   = new Point(linePointEndX, linePointEndY);
                        Pen   linePen        = new Pen(Color.Black, lineWidth);
                        gr.DrawLine(linePen, linePointStart, linePointEnd);
                        linePen.Dispose();
                        break;

                    case "text":
                        strText = pe.EntityFields.Trim();
                        font    = new Font(fontFamilyStr, fontSize, (FontStyle)fontStyle);
                        gr.DrawString(strText, font, new SolidBrush(Color.Black), x, y);    //颜色可拓展配置其他颜色
                        break;

                    case "entityfield":
                        font = new Font(fontFamilyStr, fontSize, (FontStyle)fontStyle);
                        gr.DrawString(strText, font, new SolidBrush(Color.Black), x, y);    //颜色可拓展配置其他颜色
                        break;

                    default:
                        break;
                    }
                }
                #endregion

                gr.Save();
            }
            #endregion

            #region 控制打印份数
            if (printCount > 1)
            {
                e.HasMorePages = true;
                printCount--;
            }
            #endregion
        }