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 ImageTemplate(ImageTemplateCollection parentCollection) { ParentCollection = parentCollection; Canvas = new Collection <BoxTemplate>(); Style = new StyleTemplate(); Transform = new TransformTemplate(); }
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); } }
private static void RenderImage(ImageTemplate imageTemplate, Image baseImage, object templateValues) { using (Graphics myGraphics = Graphics.FromImage(baseImage)) { myGraphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; StyleTemplate style = imageTemplate.Style; int borderWidth = style.BorderWidth; //draw rectangles for border and background if (style.BorderColor != Color.Empty && style.BackColor != Color.Empty) { Rectangle borderBounds = new Rectangle(0, 0, imageTemplate.Width, imageTemplate.Height); Rectangle backBounds = new Rectangle(borderWidth, borderWidth, imageTemplate.Width - borderWidth * 2, imageTemplate.Height - borderWidth * 2); Rectangle gradientBounds = new Rectangle(borderWidth, borderWidth, imageTemplate.Width, imageTemplate.Height + 60); using (SolidBrush solidBrush = new SolidBrush(style.BorderColor)) { myGraphics.FillRectangle(solidBrush, borderBounds); } using (LinearGradientBrush brush = new LinearGradientBrush(gradientBounds, style.BackColor, style.BackColor2, style.GradientAngle, true)) { myGraphics.FillRectangle(brush, backBounds); } } else if (style.BackColor != Color.Empty) { Rectangle backBounds = new Rectangle(borderWidth, borderWidth, imageTemplate.Width, imageTemplate.Height); Rectangle gradientBounds = new Rectangle(borderWidth, borderWidth, imageTemplate.Width, imageTemplate.Height + 60); using (LinearGradientBrush gradientBrush = new LinearGradientBrush(gradientBounds, style.BackColor, style.BackColor2, style.GradientAngle, true)) { myGraphics.FillRectangle(gradientBrush, backBounds); } } //draw background if available if (style.Image != null) { using (Image image = Image.FromFile(style.Image)) { ImageAttributes attributes = null; float a = imageTemplate.Style.ImageAlpha; if (a < 1) { float[][] matrixPoints = { new[] { 1f, 0, 0, 0, 0 }, new[] { 0, 1f, 0, 0, 0 }, new[] { 0, 0, 1f, 0, 0 }, new[] { 0, 0, 0, a, 0 }, new[] { 0, 0, 0, 0, 1f } }; ColorMatrix alphaMatrix = new ColorMatrix(matrixPoints); attributes = new ImageAttributes(); attributes.SetColorMatrix(alphaMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); } Rectangle imageBounds = new Rectangle(borderWidth, borderWidth, imageTemplate.Width - borderWidth * 2, imageTemplate.Height - borderWidth * 2); myGraphics.DrawImage(image, imageBounds, 0, 0, imageBounds.Width, imageBounds.Height, GraphicsUnit.Pixel, attributes); } } foreach (BoxTemplate box in imageTemplate.Canvas) { RenderBoxes(box, myGraphics, templateValues); } } }
/// <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) { Id = reader.GetAttribute("id"); Text = reader.GetAttribute("text"); string height = reader.GetAttribute("height"); Height = height == "max" ? Container.Height : Convert.ToInt32(height); string width = reader.GetAttribute("width"); Width = width == "max" ? Container.Width : Convert.ToInt32(width); Top = Convert.ToInt32(reader.GetAttribute("top")); Left = Convert.ToInt32(reader.GetAttribute("left")); Margin = Convert.ToInt32(reader.GetAttribute("margin")); switch (StyleTemplate.GetEnum <DockStyle>("dock", reader.GetAttribute("dock"))) { case DockStyle.Top: Top = 0; Left = 0; Width = Container.Width; break; case DockStyle.Bottom: Left = 0; Top = Container.Height - Height; Width = Container.Width; break; case DockStyle.Left: Left = 0; Top = 0; Height = Container.Height; break; case DockStyle.Right: Left = Container.Width - Width; Top = 0; Height = Container.Height; break; } XmlReader subtree = reader.ReadSubtree(); while (subtree.Read()) { XmlNodeType nodeType = reader.NodeType; if (nodeType == XmlNodeType.EndElement) { break; } if (reader.Name == "style") { ((IXmlSerializable)Style).ReadXml(reader); } if (reader.Name == "transform") { ((IXmlSerializable)Transform).ReadXml(reader); } } }
public BoxTemplate(ImageTemplate imageTemplate) { Style = new StyleTemplate(); Transform = new TransformTemplate(); Container = imageTemplate; }