private static void RenderBoxText(BoxTemplate box, StyleTemplate style, Graphics myGraphics, object templateValues) { //string format for image text StringFormat stringFormat = new StringFormat { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Near }; Dictionary <string, object> dictionary = templateValues.ToDictionary(); dictionary.Add("DATETIME", DateTime.Now); dictionary.Add("TIME", DateTime.Now.ToShortTimeString()); dictionary.Add("DATE", DateTime.Now.ToShortDateString()); dictionary.Add("MACHINENAME", Environment.MachineName); dictionary.Add("OSVERSION", Environment.OSVersion); dictionary.Add("USERDOMAINNAME", Environment.UserDomainName); dictionary.Add("USERNAME", Environment.UserName); dictionary.Add("VERSION", Environment.Version); //draw text on image string text = templateValues == null ? box.Text : FormattableObject.ToString(dictionary, box.Text, null); Rectangle textBounds = new Rectangle(box.Left, box.Top, box.Width, box.Height); using (SolidBrush textBrush = new SolidBrush(style.ForeColor)) { myGraphics.DrawString(text, style.Font, textBrush, textBounds, stringFormat); } }
public void GenOne() { ImageTemplate definition = new ImageTemplate { Height = 120, Width = 300, Id = "Green", Style = { BackColor = Color.Black } }; BoxTemplate box = new BoxTemplate(definition) { Id = "Header", Height = 20, Width = 20, Left = 20 }; definition.Canvas.Add(box); box = new BoxTemplate(definition) { Id = "Description", Height = 20, Width = 20, Left = 20, Top = 20, Text = "Welcome Home!", Style = { BackColor = Color.Red, ForeColor = Color.White, Image = "/images/logo.png", ImageRepeat = ImageRepeat.Horizontal } }; definition.Canvas.Add(box); string expected = definition.ToXml(); ImageTemplate imageTemplate = ImageTemplate.FromXml(expected); string actual = imageTemplate.ToXml(); StringBuilder message = new StringBuilder(100); message.AppendLine("------- ORIGINAL XML -------"); message.AppendLine(); message.AppendLine(expected); message.AppendLine(); message.AppendLine("------- ROUNDTRIP XML -------"); message.AppendLine(); message.AppendLine(actual); message.AppendLine(); Assert.AreEqual(expected, actual, message.ToString()); }
/// <summary> /// Generates an object from its XML representation. /// </summary> /// <param name="reader"> /// The <see cref="XmlReader"></see> stream from which /// the object is deserialized. /// </param> void IXmlSerializable.ReadXml(XmlReader reader) { if ((reader.NodeType == XmlNodeType.None || reader.Name != "imageTemplate") && !reader.ReadToFollowing("imageTemplate")) { throw new InvalidOperationException("Expected element 'imageTemplate'."); } Id = reader.GetAttribute("id"); if (string.IsNullOrEmpty(Id)) { throw new InvalidOperationException("The required attribute 'Id' was not found."); } string width = reader.GetAttribute("width"); if (!string.IsNullOrEmpty(width)) { Width = Convert.ToInt32(width); } string height = reader.GetAttribute("height"); if (!string.IsNullOrEmpty(height)) { Height = Convert.ToInt32(height); } string inherits = reader.GetAttribute("inherits"); if (!string.IsNullOrEmpty(inherits)) { Inherits = inherits; } if (reader.ReadToDescendant("style")) { ((IXmlSerializable)Style).ReadXml(reader); if (!reader.ReadToFollowing("canvas")) { throw new InvalidOperationException("Expected the element 'canvas' to follow the 'imageTemplate' and 'style' elements."); } } if (reader.Name != "canvas") { throw new InvalidOperationException( "Expected the element 'style' or 'canvas' to follow the 'imageTemplate' element."); } while (reader.Read() && reader.Name == "box") { BoxTemplate box = new BoxTemplate(this); Canvas.Add(box); ((IXmlSerializable)box).ReadXml(reader); } }
private static void RenderBoxBackground(BoxTemplate box, StyleTemplate style, Graphics myGraphics) { Rectangle backBounds = new Rectangle(box.Left, box.Top, box.Width, box.Height); Rectangle gradientBounds = new Rectangle(box.Left, box.Top, box.Width, box.Height + 60); using (LinearGradientBrush gradientBrush = new LinearGradientBrush(gradientBounds, style.BackColor, style.BackColor2, style.GradientAngle, true)) { myGraphics.FillRectangle(gradientBrush, backBounds); } }
private static void RenderBoxBorderAndBackground(BoxTemplate box, StyleTemplate style, int borderWidth, Graphics myGraphics) { Rectangle borderBounds = new Rectangle(0, 0, box.Width, box.Height); Rectangle backBounds = new Rectangle(borderWidth, borderWidth, box.Width - borderWidth * 2, box.Height - borderWidth * 2); using (SolidBrush brush = new SolidBrush(style.BorderColor)) { myGraphics.FillRectangle(brush, borderBounds); } using (LinearGradientBrush gradientBrush = new LinearGradientBrush(new Rectangle(style.BorderWidth, style.BorderWidth, box.Width, box.Height + 60), style.BackColor, style.BackColor2, style.GradientAngle, true)) { myGraphics.FillRectangle(gradientBrush, backBounds); } }
private static void RenderBoxImage(BoxTemplate box, StyleTemplate style, Graphics myGraphics, object templateValues) { //find image max size int maxSize = box.Width < box.Height ? box.Width : box.Height; Dictionary <string, object> dictionary = templateValues.ToDictionary(); string path = templateValues == null ? style.Image : FormattableObject.ToString(dictionary, style.Image, null); using (Image image = Image.FromFile(path)) using (Image thumbnail = GetThumbnail(image, maxSize)) using (Image rotated = RotateImage(thumbnail, thumbnail.Width / 2, thumbnail.Height / 2, style.Rotation, true)) { Rectangle imageRectangle = new Rectangle(new Point(box.Left, box.Top), new Size(rotated.Width, rotated.Height)); myGraphics.DrawImage(rotated, imageRectangle, 0, 0, rotated.Width, rotated.Height, GraphicsUnit.Pixel); } }
private static void RenderBoxes(BoxTemplate box, Graphics myGraphics, object templateValues) { StyleTemplate style = box.Style; int borderWidth = style.BorderWidth; if (style.BorderColor != Color.Empty && style.BackColor != Color.Empty) { RenderBoxBorderAndBackground(box, style, borderWidth, myGraphics); } else if (style.BackColor != Color.Empty) { RenderBoxBackground(box, style, myGraphics); } if (style.Image != null) { RenderBoxImage(box, style, myGraphics, templateValues); } if (box.Text != null) { RenderBoxText(box, style, myGraphics, templateValues); } }
public void GenMany() { ImageTemplate definition = new ImageTemplate(); definition.Height = 120; definition.Width = 300; definition.Id = "Green"; definition.Style.BackColor = Color.Black; BoxTemplate box = new BoxTemplate(definition); box.Id = "Header"; box.Height = 20; box.Width = 20; box.Left = 20; definition.Canvas.Add(box); box.Style.BackColor = Color.Red; box.Style.ForeColor = Color.White; box.Style.Image = "/images/logo.png"; box.Style.ImageRepeat = ImageRepeat.Horizontal; box = new BoxTemplate(definition); box.Id = "Description"; box.Height = 20; box.Width = 20; box.Left = 20; box.Top = 20; box.Text = "Welcome Home!"; definition.Canvas.Add(box); box.Style.BackColor = Color.Red; box.Style.ForeColor = Color.White; box.Style.Image = "/images/logo.png"; box.Style.ImageRepeat = ImageRepeat.Horizontal; //------------------------------------------------------------------- ImageTemplate definition2 = new ImageTemplate(); definition2.Height = 120; definition2.Width = 300; definition2.Id = "Blue"; definition2.Style.BackColor = Color.Black; box = new BoxTemplate(definition2); box.Id = "Header"; box.Height = 20; box.Width = 20; box.Left = 20; definition2.Canvas.Add(box); box.Style.BackColor = Color.Red; box.Style.ForeColor = Color.White; box.Style.Image = "/images/logo.png"; box.Style.ImageRepeat = ImageRepeat.Horizontal; box = new BoxTemplate(definition2); box.Id = "Description"; box.Height = 20; box.Width = 20; box.Left = 20; box.Top = 20; box.Text = "Welcome Home!"; definition2.Canvas.Add(box); box.Style.BackColor = Color.Red; box.Style.ForeColor = Color.White; box.Style.Image = "/images/logo.png"; box.Style.ImageRepeat = ImageRepeat.Horizontal; ImageTemplateCollection definitions = new ImageTemplateCollection(); definitions.Add(definition); definitions.Add(definition2); string expected = definitions.ToXml(); string actual = ImageTemplateCollection.FromXml(expected).ToXml(); StringBuilder message = new StringBuilder(100); message.AppendLine("------- ORIGINAL XML -------"); message.AppendLine(); message.AppendLine(expected); message.AppendLine(); message.AppendLine("------- ROUNDTRIP XML -------"); message.AppendLine(); message.AppendLine(actual); message.AppendLine(); Assert.AreEqual(expected, actual, message.ToString()); }