/// <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); }
/// <summary> /// 对变量属性进行赋值 /// </summary> /// <param name="drawText"></param> /// <param name="xmlAttr"></param> /// <param name="param"></param> void AttrVarSet(DrawText drawText, XmlAttribute xmlAttr, Dictionary <string, object> param) { if (param.ContainsKey(xmlAttr.Value)) { try { if (string.Equals(xmlAttr.Name, "ref-Width", StringComparison.CurrentCultureIgnoreCase)) { drawText.Width = (int)param[xmlAttr.Value]; } else if (string.Equals(xmlAttr.Name, "ref-Height", StringComparison.CurrentCultureIgnoreCase)) { drawText.Height = (int)param[xmlAttr.Value]; } else if (string.Equals(xmlAttr.Name, "ref-Left", StringComparison.CurrentCultureIgnoreCase)) { drawText.Left = (int)param[xmlAttr.Value]; } else if (string.Equals(xmlAttr.Name, "ref-Top", StringComparison.CurrentCultureIgnoreCase)) { drawText.Top = (int)param[xmlAttr.Value]; } else if (string.Equals(xmlAttr.Name, "ref-Text", StringComparison.CurrentCultureIgnoreCase)) { drawText.Text = (string)param[xmlAttr.Value]; } else if (string.Equals(xmlAttr.Name, "ref-FontFamily", StringComparison.CurrentCultureIgnoreCase)) { drawText.FontFamily = (string)param[xmlAttr.Value]; } else if (string.Equals(xmlAttr.Name, "ref-FontSize", StringComparison.CurrentCultureIgnoreCase)) { drawText.FontSize = (int)param[xmlAttr.Value]; } else if (string.Equals(xmlAttr.Name, "ref-FontStyle", StringComparison.CurrentCultureIgnoreCase)) { drawText.FontStyle = (FontStyle)param[xmlAttr.Value]; } else if (string.Equals(xmlAttr.Name, "ref-FontColor", StringComparison.CurrentCultureIgnoreCase)) { drawText.FontColor = (Color)param[xmlAttr.Value]; } } catch (Exception ex) { Console.WriteLine($"设置动态属性时出错,错误原因:{ex.Message}"); } } }
/// <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); }
/* * <drawImg width="" height="" left="" top="" backgroundPath="" backgroundUrl="" rotate=""> * <drawImg width="" height="" left="" top="" backgroundPath="" backgroundUrl="" rotate=""> * * </drawImg> * <drawText width="" height="" left="" top="" text="" fontFamily="" fontSize="" fontStyle=""> * * </drawText> * </drawImg> */ /// <summary> /// 对常量属性进行赋值 /// </summary> /// <param name="drawImg"></param> /// <param name="xmlAttr"></param> void AttrConstSet(DrawText drawText, XmlAttribute xmlAttr) { if (string.Equals(xmlAttr.Name, "Width", StringComparison.CurrentCultureIgnoreCase)) { int width; if (int.TryParse(xmlAttr.Value, out width)) { drawText.Width = width; } } else if (string.Equals(xmlAttr.Name, "Height", StringComparison.CurrentCultureIgnoreCase)) { int height; if (int.TryParse(xmlAttr.Value, out height)) { drawText.Height = height; } } else if (string.Equals(xmlAttr.Name, "Left", StringComparison.CurrentCultureIgnoreCase)) { int left; if (int.TryParse(xmlAttr.Value, out left)) { drawText.Left = left; } } else if (string.Equals(xmlAttr.Name, "Top", StringComparison.CurrentCultureIgnoreCase)) { int top; if (int.TryParse(xmlAttr.Value, out top)) { drawText.Top = top; } } else if (string.Equals(xmlAttr.Name, "Text", StringComparison.CurrentCultureIgnoreCase)) { drawText.Text = xmlAttr.Value; } else if (string.Equals(xmlAttr.Name, "FontFamily", StringComparison.CurrentCultureIgnoreCase)) { drawText.FontFamily = xmlAttr.Value; } else if (string.Equals(xmlAttr.Name, "FontSize", StringComparison.CurrentCultureIgnoreCase)) { int fontSize; if (int.TryParse(xmlAttr.Value, out fontSize)) { drawText.FontSize = fontSize; } } else if (string.Equals(xmlAttr.Name, "FontStyle", StringComparison.CurrentCultureIgnoreCase)) { foreach (var item in Enum.GetValues(typeof(FontStyle))) { if (string.Equals(xmlAttr.Value, item.ToString(), StringComparison.CurrentCultureIgnoreCase)) { drawText.FontStyle = (FontStyle)item; break; } } } else if (string.Equals(xmlAttr.Name, "FontColor", StringComparison.CurrentCultureIgnoreCase)) { string color = xmlAttr.Value.Trim(); drawText.FontColor = ColorDescToColor(color); } }