/// <summary> /// Loads the selected layoutfile /// </summary> /// <param name="filename">The layout file to load</param> public void LoadLayout(string filename) { //Open the model xml document XmlDocument layoutXmlDoc = new XmlDocument(); layoutXmlDoc.Load(filename); XmlElement root = layoutXmlDoc.DocumentElement; List<LayoutElement> loadList = new List<LayoutElement>(); if (root != null) { if (root.Name == "MapWindowLayout") { //This creates instances of all the elements XmlNode child = root.LastChild; while (child != null) { LayoutElement newLe = null; switch (child.ChildNodes[0].Name) { case "Bitmap": newLe = new LayoutBitmap(); break; case "Legend": newLe = CreateLegendElement(); break; case "Map": newLe = CreateMapElement(); break; case "NorthArrow": newLe = new LayoutNorthArrow(); break; case "Rectangle": newLe = new LayoutRectangle(); break; case "ScaleBar": newLe = CreateScaleBarElement(); break; case "Text": newLe = new LayoutText(); break; } if (newLe != null) { newLe.Name = child.Attributes["Name"].Value; newLe.Invalidated += LeInvalidated; newLe.Rectangle = new RectangleF(float.Parse(child.Attributes["RectangleX"].Value, System.Globalization.CultureInfo.InvariantCulture), float.Parse(child.Attributes["RectangleY"].Value, System.Globalization.CultureInfo.InvariantCulture), float.Parse(child.Attributes["RectangleWidth"].Value, System.Globalization.CultureInfo.InvariantCulture), float.Parse(child.Attributes["RectangleHeight"].Value, System.Globalization.CultureInfo.InvariantCulture)); newLe.ResizeStyle = (ResizeStyle)Enum.Parse(typeof(ResizeStyle), child.Attributes["ResizeStyle"].Value); loadList.Insert(0,newLe); } child = child.PreviousSibling; } //Since some of the elements may be dependant on elements already being added we add their other properties after we add them all child = root.LastChild; for (int i = loadList.Count - 1; i >= 0; i--) { if (child != null) { XmlNode innerChild = child.ChildNodes[0]; if (loadList[i] is LayoutBitmap) { LayoutBitmap lb = loadList[i] as LayoutBitmap; if (lb != null) { lb.Filename = innerChild.Attributes["Filename"].Value; lb.PreserveAspectRatio = Convert.ToBoolean(innerChild.Attributes["PreserveAspectRatio"].Value); lb.Draft = Convert.ToBoolean(innerChild.Attributes["Draft"].Value); } } else if (loadList[i] is LayoutLegend) { LayoutLegend ll = loadList[i] as LayoutLegend; if (ll != null) { ll.LayoutControl = this; ll.TextHint = (System.Drawing.Text.TextRenderingHint)Enum.Parse(typeof(System.Drawing.Text.TextRenderingHint), innerChild.Attributes["TextHint"].Value); ll.Color = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromInvariantString(innerChild.Attributes["Color"].Value); ll.Font = (Font)TypeDescriptor.GetConverter(typeof(Font)).ConvertFromString(innerChild.Attributes["Font"].Value); } int mapIndex = Convert.ToInt32(innerChild.Attributes["Map"].Value); if (mapIndex >= 0) if (ll != null) ll.Map = loadList[mapIndex] as LayoutMap; string layStr = innerChild.Attributes["Layers"].Value; List<int> layers = new List<int>(); while (layStr.EndsWith("|")) { layStr = layStr.TrimEnd("|".ToCharArray()); layers.Add((int)TypeDescriptor.GetConverter(typeof(int)).ConvertFromString(layStr.Substring(layStr.LastIndexOf("|") + 1))); layStr = layStr.Substring(0, layStr.LastIndexOf("|") + 1); } if (ll != null) { ll.NumColumns = (int)TypeDescriptor.GetConverter(typeof(int)).ConvertFromString(innerChild.Attributes["NumColumns"].Value); ll.Layers = layers; } } else if (loadList[i] is LayoutMap) { LayoutMap lm = loadList[i] as LayoutMap; Geometries.Envelope env = new Geometries.Envelope(); env.Minimum.X = (double)TypeDescriptor.GetConverter(typeof(double)).ConvertFromInvariantString(innerChild.Attributes["EnvelopeXmin"].Value); env.Minimum.Y = (double)TypeDescriptor.GetConverter(typeof(double)).ConvertFromInvariantString(innerChild.Attributes["EnvelopeYmin"].Value); env.Maximum.X = (double)TypeDescriptor.GetConverter(typeof(double)).ConvertFromInvariantString(innerChild.Attributes["EnvelopeXmax"].Value); env.Maximum.Y = (double)TypeDescriptor.GetConverter(typeof(double)).ConvertFromInvariantString(innerChild.Attributes["EnvelopeYmax"].Value); if (lm != null) lm.Envelope = env; } else if (loadList[i] is LayoutNorthArrow) { LayoutNorthArrow na = loadList[i] as LayoutNorthArrow; if (na != null) { na.Color = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromInvariantString(innerChild.Attributes["Color"].Value); na.NorthArrowStyle = (NorthArrowStyle)Enum.Parse(typeof(NorthArrowStyle), innerChild.Attributes["Style"].Value); if (innerChild.Attributes["Rotation"] != null) { na.Rotation = (float)TypeDescriptor.GetConverter(typeof(float)).ConvertFromInvariantString(innerChild.Attributes["Rotation"].Value); } } } else if (loadList[i] is LayoutRectangle) { LayoutRectangle lr = loadList[i] as LayoutRectangle; if (lr != null) { lr.Color = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString(innerChild.Attributes["Color"].Value); lr.BackColor = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString(innerChild.Attributes["BackColor"].Value); lr.OutlineWidth = Convert.ToInt32(innerChild.Attributes["OutlineWidth"].Value); } } else if (loadList[i] is LayoutScaleBar) { LayoutScaleBar lsc = loadList[i] as LayoutScaleBar; if (lsc != null) { lsc.LayoutControl = this; lsc.TextHint = (System.Drawing.Text.TextRenderingHint)Enum.Parse(typeof(System.Drawing.Text.TextRenderingHint), innerChild.Attributes["TextHint"].Value); lsc.Color = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString(innerChild.Attributes["Color"].Value); lsc.Font = (Font)TypeDescriptor.GetConverter(typeof(Font)).ConvertFromString(innerChild.Attributes["Font"].Value); lsc.BreakBeforeZero = Convert.ToBoolean(innerChild.Attributes["BreakBeforeZero"].Value); lsc.NumberOfBreaks = Convert.ToInt32(innerChild.Attributes["NumberOfBreaks"].Value); lsc.Unit = (ScaleBarUnits)Enum.Parse(typeof(ScaleBarUnits), innerChild.Attributes["Unit"].Value); lsc.UnitText = innerChild.Attributes["UnitText"].Value; } int mapIndex = Convert.ToInt32(innerChild.Attributes["Map"].Value); if (mapIndex >= 0) if (lsc != null) lsc.Map = loadList[mapIndex] as LayoutMap; } else if (loadList[i] is LayoutText) { LayoutText lt = loadList[i] as LayoutText; if (lt != null) { lt.TextHint = (System.Drawing.Text.TextRenderingHint)Enum.Parse(typeof(System.Drawing.Text.TextRenderingHint), innerChild.Attributes["TextHint"].Value); lt.Color = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString(innerChild.Attributes["Color"].Value); lt.Font = (Font)TypeDescriptor.GetConverter(typeof(Font)).ConvertFromString(innerChild.Attributes["Font"].Value); lt.ContentAlignment = (ContentAlignment)TypeDescriptor.GetConverter(typeof(ContentAlignment)).ConvertFromString(innerChild.Attributes["ContentAlignment"].Value); lt.Text = innerChild.Attributes["Text"].Value; } } } if (child != null) child = child.PreviousSibling; } _layoutElements.Clear(); _selectedLayoutElements.Clear(); _layoutElements.InsertRange(0, loadList); Filename = filename; Invalidate(); OnElementsChanged(null); } } }
/// <summary> /// Converts a selected layout element into a bitmap and saves it a the specified location removing the old element and replacing it /// </summary> /// <param name="le"></param> /// <param name="filename"></param> public virtual void ConvertElementToBitmap(LayoutElement le, string filename) { if (le is LayoutBitmap) return; Bitmap temp = new Bitmap(Convert.ToInt32(le.Size.Width * 3), Convert.ToInt32(le.Size.Height * 3), System.Drawing.Imaging.PixelFormat.Format32bppArgb); temp.SetResolution(96, 96); temp.MakeTransparent(); Graphics g = Graphics.FromImage(temp); g.PageUnit = GraphicsUnit.Pixel; g.ScaleTransform(300F/100F, 300F/100F); g.TranslateTransform(-le.LocationF.X,-le.LocationF.Y); le.Draw(g,true); g.Dispose(); temp.SetResolution(300, 300); temp.Save(filename); temp.Dispose(); LayoutBitmap newLb = new LayoutBitmap(); newLb.Rectangle = le.Rectangle; newLb.Name = le.Name; newLb.Filename = filename; _layoutElements.Insert(_layoutElements.IndexOf(le), newLb); _layoutElements.Remove(le); _selectedLayoutElements.Insert(_selectedLayoutElements.IndexOf(le), newLb); _selectedLayoutElements.Remove(le); OnSelectionChanged(null); Invalidate(); }