Exemple #1
0
        /// <summary>
        /// 把xml转化为图片
        /// </summary>
        /// <param name="xml"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public Image XmlToImage(string xml, Dictionary <string, object> param = null)
        {
            DrawImg drawImg = XmlToDrawImg(xml, param);
            Image   image   = DrawTree(drawImg);

            Dispose(drawImg);
            return(image);
        }
Exemple #2
0
        /// <summary>
        /// 根据drawImg树结构进行绘图
        /// </summary>
        /// <param name="drawImg">drawImg树结构</param>
        /// <returns>返回绘制的图片</returns>
        public Image DrawTree(DrawImg drawImg)
        {
            Bitmap bitmap = new Bitmap(drawImg.Width, drawImg.Height);

            using (Graphics g = Graphics.FromImage(bitmap))
            {
                Image background = null;
                if (!string.IsNullOrEmpty(drawImg.BackgroundUrl))
                {
                    WebResponse webResponse = HttpWebRequestHelper.HttpGet(drawImg.BackgroundUrl);
                    background = webResponse.GetBitmap();
                }
                else if (!string.IsNullOrEmpty(drawImg.BackgroundPath))
                {
                    background = Image.FromFile(drawImg.BackgroundPath);
                }
                else if (drawImg.BackgroundImage != null)
                {
                    background = drawImg.BackgroundImage;
                }
                if (drawImg.BackgroundColor != null)
                {
                    g.FillRectangle(new SolidBrush(drawImg.BackgroundColor), new Rectangle(0, 0, drawImg.Width, drawImg.Height));
                }
                if (background != null)
                {
                    g.DrawImage(background, new Rectangle(0, 0, drawImg.Width, drawImg.Height));
                }
                if (drawImg.DrawList != null && drawImg.DrawList.Count > 0)
                {
                    foreach (Draw childDraw in drawImg.DrawList)
                    {
                        if (childDraw.GetType() == typeof(DrawImg))
                        {
                            DrawImg tempDrawImg = (DrawImg)childDraw;
                            using (Image childImage = DrawTree(tempDrawImg))
                            {
                                childImage.RotateFlip(tempDrawImg.Rotate);
                                g.DrawImage(childImage, new Rectangle(childDraw.Left, childDraw.Top, childImage.Width, childImage.Height));
                            }
                        }
                        else if (childDraw.GetType() == typeof(DrawText))
                        {
                            DrawText   tempDrawText = (DrawText)childDraw;
                            Font       font         = new Font(tempDrawText.FontFamily, tempDrawText.FontSize, tempDrawText.FontStyle);
                            RectangleF rectangleF   = new RectangleF(tempDrawText.Left, tempDrawText.Top, tempDrawText.Width, tempDrawText.Height);
                            g.DrawString(tempDrawText.Text, font, new SolidBrush(tempDrawText.FontColor), rectangleF);
                        }
                    }
                }
                g.Save();
            }
            return(bitmap);
        }
Exemple #3
0
 /// <summary>
 /// 对常量属性进行赋值
 /// </summary>
 /// <param name="drawImg"></param>
 /// <param name="xmlAttr"></param>
 void AttrConstSet(DrawImg drawImg, XmlAttribute xmlAttr)
 {
     if (string.Equals(xmlAttr.Name, "Width", StringComparison.CurrentCultureIgnoreCase))
     {
         int width;
         if (int.TryParse(xmlAttr.Value, out width))
         {
             drawImg.Width = width;
         }
     }
     else if (string.Equals(xmlAttr.Name, "Height", StringComparison.CurrentCultureIgnoreCase))
     {
         int height;
         if (int.TryParse(xmlAttr.Value, out height))
         {
             drawImg.Height = height;
         }
     }
     else if (string.Equals(xmlAttr.Name, "Left", StringComparison.CurrentCultureIgnoreCase))
     {
         int left;
         if (int.TryParse(xmlAttr.Value, out left))
         {
             drawImg.Left = left;
         }
     }
     else if (string.Equals(xmlAttr.Name, "Top", StringComparison.CurrentCultureIgnoreCase))
     {
         int top;
         if (int.TryParse(xmlAttr.Value, out top))
         {
             drawImg.Top = top;
         }
     }
     else if (string.Equals(xmlAttr.Name, "BackgroundPath", StringComparison.CurrentCultureIgnoreCase))
     {
         drawImg.BackgroundPath = xmlAttr.Value;
     }
     else if (string.Equals(xmlAttr.Name, "BackgroundUrl", StringComparison.CurrentCultureIgnoreCase))
     {
         drawImg.BackgroundUrl = xmlAttr.Value;
     }
     else if (string.Equals(xmlAttr.Name, "Rotate", StringComparison.CurrentCultureIgnoreCase))
     {
         foreach (var item in Enum.GetValues(typeof(RotateFlipType)))
         {
             if (string.Equals(xmlAttr.Value, item.ToString(), StringComparison.CurrentCultureIgnoreCase))
             {
                 drawImg.Rotate = (RotateFlipType)item;
                 break;
             }
         }
     }
 }
Exemple #4
0
 /// <summary>
 /// 对变量属性进行赋值
 /// </summary>
 /// <param name="drawImg"></param>
 /// <param name="xmlAttr"></param>
 /// <param name="param"></param>
 void AttrVarSet(DrawImg drawImg, XmlAttribute xmlAttr, Dictionary <string, object> param)
 {
     if (param.ContainsKey(xmlAttr.Value))
     {
         try
         {
             if (string.Equals(xmlAttr.Name, "ref-Width", StringComparison.CurrentCultureIgnoreCase))
             {
                 drawImg.Width = (int)param[xmlAttr.Value];
             }
             else if (string.Equals(xmlAttr.Name, "ref-Height", StringComparison.CurrentCultureIgnoreCase))
             {
                 drawImg.Height = (int)param[xmlAttr.Value];
             }
             else if (string.Equals(xmlAttr.Name, "ref-Left", StringComparison.CurrentCultureIgnoreCase))
             {
                 drawImg.Left = (int)param[xmlAttr.Value];
             }
             else if (string.Equals(xmlAttr.Name, "ref-Top", StringComparison.CurrentCultureIgnoreCase))
             {
                 drawImg.Top = (int)param[xmlAttr.Value];
             }
             else if (string.Equals(xmlAttr.Name, "ref-BackgroundPath", StringComparison.CurrentCultureIgnoreCase))
             {
                 drawImg.BackgroundPath = (string)param[xmlAttr.Value];
             }
             else if (string.Equals(xmlAttr.Name, "ref-BackgroundUrl", StringComparison.CurrentCultureIgnoreCase))
             {
                 drawImg.BackgroundUrl = (string)param[xmlAttr.Value];
             }
             else if (string.Equals(xmlAttr.Name, "ref-BackgroundImage", StringComparison.CurrentCultureIgnoreCase))
             {
                 drawImg.BackgroundImage = (Image)param[xmlAttr.Value];
             }
             else if (string.Equals(xmlAttr.Name, "ref-BackgroundColor", StringComparison.CurrentCultureIgnoreCase))
             {
                 drawImg.BackgroundColor = (Color)param[xmlAttr.Value];
             }
             else if (string.Equals(xmlAttr.Name, "ref-Rotate", StringComparison.CurrentCultureIgnoreCase))
             {
                 drawImg.Rotate = (RotateFlipType)param[xmlAttr.Value];
             }
         }
         catch (Exception ex)
         {
             Console.WriteLine($"设置动态属性时出错,错误原因:{ex.Message}");
         }
     }
 }
Exemple #5
0
        /// <summary>
        /// 把节点解析成DrawImg对象
        /// </summary>
        /// <param name="xmlNodeList"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        List <Draw> NodeToDrawImg(XmlNodeList xmlNodeList, Dictionary <string, object> param = null)
        {
            List <Draw> drawList       = new List <Draw>();
            bool        paramIsNotNull = param != null;

            foreach (XmlNode xmlNode in xmlNodeList)
            {
                if (xmlNode.NodeType == XmlNodeType.Element)
                {
                    if (string.Equals(xmlNode.Name, "drawImg", StringComparison.CurrentCultureIgnoreCase))
                    {
                        DrawImg drawImg = new DrawImg();
                        foreach (XmlAttribute xmlAttr in xmlNode.Attributes)
                        {
                            if (paramIsNotNull)
                            {
                                AttrVarSet(drawImg, xmlAttr, param);
                            }
                            AttrConstSet(drawImg, xmlAttr);
                        }
                        drawList.Add(drawImg);
                        if (xmlNode.HasChildNodes)
                        {
                            drawImg.DrawList = NodeToDrawImg(xmlNode.ChildNodes, param);
                        }
                    }
                    else if (string.Equals(xmlNode.Name, "drawText", StringComparison.CurrentCultureIgnoreCase))
                    {
                        DrawText drawText = new DrawText();
                        foreach (XmlAttribute xmlAttr in xmlNode.Attributes)
                        {
                            if (paramIsNotNull)
                            {
                                AttrVarSet(drawText, xmlAttr, param);
                            }
                            AttrConstSet(drawText, xmlAttr);
                        }
                        drawList.Add(drawText);
                    }
                }
            }
            return(drawList);
        }
Exemple #6
0
        /// <summary>
        /// 执行与释放或重置非托管资源相关的应用程序定义的任务
        /// </summary>
        public void Dispose(DrawImg drawImg)
        {
            bool hasChild = drawImg.DrawList != null && drawImg.DrawList.Count > 0;

            if (hasChild)
            {
                foreach (Draw childDraw in drawImg.DrawList)
                {
                    if (childDraw.GetType() == typeof(DrawImg))
                    {
                        DrawImg tempDrawImg = (DrawImg)childDraw;
                        using (tempDrawImg.BackgroundImage) { }
                        Dispose(tempDrawImg);
                    }
                }
            }
            else
            {
                using (drawImg.BackgroundImage) { }
            }
        }