private void DrawMultiLegend(ref Graphics g, Legends _Legends, int LegendWidth, int LegendHeight, bool ShowPointLegend, bool showMissingDataInLegend, int MultiLegendIndex, LegendsSequenceOrder legendSequenceOrder) { int i; SolidBrush BrTitle = new SolidBrush(m_LegendBodyColor); Legend _Legend; string LegendCaption = string.Empty; SizeF LegendTitleSize = g.MeasureString(m_LegendTitle, m_LegendFont); int LegendLeftPos = 0; float ImageRowHeight = LegendTitleSize.Height; // image height set in single row when width exceeds then wrap in next row //change Increment int SetOnce = 0; //endchange switch (m_ThemeType) { case ThemeType.Symbol: object BrLegend; StringFormat _StringFormat = new StringFormat(); _StringFormat.Alignment = StringAlignment.Center; _StringFormat.LineAlignment = StringAlignment.Center; _StringFormat.FormatFlags = StringFormatFlags.NoClip; for (i = 0; i <= _Legends.Count - 1; i++) { bool DrawLegendRequired = true; //- Skip MissingData Legend if not required if (i == m_Legends.Count - 1 && showMissingDataInLegend == false) { DrawLegendRequired = false; } if (DrawLegendRequired) { _Legend = _Legends[i]; LegendCaption = string.Empty; //*** Pick Legend Title from Source Legend - [Caption] Doublespace [Title] if (this.ShowLegendCaption) { LegendCaption = _Legend.Caption + " "; } else if (showMissingDataInLegend && i == m_Legends.Count - 1) { //- Show MissingData for Last Legend if Caption is blank LegendCaption = _Legend.Caption + " "; } if (this.ShowLegendRange && (!(string.IsNullOrEmpty(_Legend.Title)))) { LegendCaption += _Legend.Title; } if (this.ShowAreaCount) { LegendCaption += " (" + Convert.ToString(_Legend.ShapeCount) + ")"; } //-Draw Symbol marchar if (legendSequenceOrder == LegendsSequenceOrder.SingleRow) { _StringFormat.Alignment = StringAlignment.Near; //- Show all symbols in single row (Horizontally) //- Starting left position of Legend's Color box LegendLeftPos += 10; //-show Symbol box adjacent (right) to previous Legend if (_Legend.SymbolImage == "" || File.Exists(_Legend.SymbolImage) == false) { BrLegend = new SolidBrush(_Legend.Color); g.DrawString(_Legend.MarkerChar.ToString(), _Legend.MarkerFont, (SolidBrush)BrLegend, LegendLeftPos, LegendTitleSize.Height + 5 + (_Legend.MarkerFont.Size / 3), _StringFormat); //_StringFormat LegendLeftPos += (int)(g.MeasureString(_Legend.MarkerChar.ToString(), _Legend.MarkerFont).Width); } else { BrLegend = Image.FromFile(_Legend.SymbolImage); g.DrawImage((Image)BrLegend, LegendLeftPos, LegendTitleSize.Height + 5 + 6, Math.Min(16, ((Image)BrLegend).Width), Math.Min(13, ((Image)BrLegend).Height)); LegendLeftPos += LegendWidth; } } else { //-Show all Symbols in single column if (_Legend.SymbolImage == "" || File.Exists(_Legend.SymbolImage) == false) { BrLegend = new SolidBrush(_Legend.Color); g.DrawString(_Legend.MarkerChar.ToString(), _Legend.MarkerFont, (SolidBrush)BrLegend, 30, LegendTitleSize.Height + 5 + (_Legend.MarkerFont.Size / 2) + (i * LegendHeight), _StringFormat); //_StringFormat } else { BrLegend = Image.FromFile(_Legend.SymbolImage); g.DrawImage((Image)BrLegend, 10, LegendTitleSize.Height + 5 + (i * LegendHeight) + 6, Math.Min(16, ((Image)BrLegend).Width), Math.Min(13, ((Image)BrLegend).Height)); } } if (legendSequenceOrder == LegendsSequenceOrder.SingleColumn) { //-Show Legend Caption or Range g.DrawString(LegendCaption, m_LegendBodyFont, BrTitle, LegendWidth + 20, LegendTitleSize.Height + 5 + (i * LegendHeight)); } else { //- if in single row, then draw caption next to Symbol Box LegendLeftPos += 10; g.DrawString(LegendCaption.Trim(), m_LegendBodyFont, BrTitle, LegendLeftPos, LegendTitleSize.Height + 5); //- For next Legend, increment left position by caption width. LegendLeftPos += (int)(g.MeasureString(LegendCaption.Trim(), m_LegendBodyFont).Width); } //Range Title } } BrLegend = new SolidBrush(Color.Black); // BrLegend object is equalled to SolidBrush so that .Dispose() can be used. SolidBrush has no significance here. if ((BrLegend != null)) { ((SolidBrush)BrLegend).Dispose(); } _StringFormat.Dispose(); break; //TODO Handle MultiColorTheme case case ThemeType.Label: int PreviousLegendHeight = 0; SizeF LegendSize; BrLegend = new SolidBrush(Color.Black); for (i = 0; i <= _Legends.Count - 1; i++) { _Legend = _Legends[i]; LegendCaption = string.Empty; //*** Pick Legend Title from Source Legend - [Caption] Doublespace [Title] if (this.ShowLegendCaption) { LegendCaption = _Legend.Caption + " "; } else if (showMissingDataInLegend && i == m_Legends.Count - 1) { //- Show MissingData for Last Legend if Caption is blank LegendCaption = _Legend.Caption + " "; } if (this.ShowLegendRange && (!(string.IsNullOrEmpty(_Legend.Title)))) { LegendCaption += _Legend.Title; } if (this.ShowAreaCount) { LegendCaption += " (" + Convert.ToString(_Legend.ShapeCount) + ")"; } LegendSize = g.MeasureString(LegendCaption, _Legend.MarkerFont); LegendHeight = (int)(LegendSize.Height); LegendWidth = (int)(LegendSize.Width); BrLegend = new SolidBrush(_Legend.Color); //accomodate legend size to display large text g.SetClip(new Rectangle(0, (int)LegendTitleSize.Height + PreviousLegendHeight + 5, (int)(15 + LegendWidth), LegendHeight)); g.DrawString(LegendCaption, _Legend.MarkerFont, (SolidBrush)BrLegend, 10, LegendTitleSize.Height + PreviousLegendHeight + 5); g.ResetClip(); PreviousLegendHeight += LegendHeight; } ((SolidBrush)BrLegend).Dispose(); break; //TODO Handle MultiColorTheme case case ThemeType.Color: case ThemeType.Hatch: LegendLeftPos = 0; for (i = 0; i <= _Legends.Count - 1; i++) { bool DrawLegendRequired = true; //- Skip MissingData Legend if not required if (i == m_Legends.Count - 1 && showMissingDataInLegend == false) { DrawLegendRequired = false; } if (DrawLegendRequired) { _Legend = _Legends[i]; LegendCaption = string.Empty; //*** Pick Title from Source Legend //--Start Change C1 - display - <Caption>Doublespace<Title> if (this.ShowLegendCaption) { LegendCaption = _Legend.Caption + " "; } else if (showMissingDataInLegend && i == m_Legends.Count - 1) { //- Show MissingData for Last Legend if Caption is blank LegendCaption = _Legend.Caption + " "; } if (this.ShowLegendRange && (!(string.IsNullOrEmpty(_Legend.Title)))) { LegendCaption += _Legend.Title; } if (this.ShowAreaCount) { LegendCaption += " (" + Convert.ToString(_Legend.ShapeCount) + ")"; } if (_Legend.FillStyle == FillStyle.Solid) { BrLegend = new SolidBrush(_Legend.Color); } else if (_Legend.FillStyle == FillStyle.Transparent) { BrLegend = new SolidBrush(Color.Transparent); } else { BrLegend = new HatchBrush((HatchStyle)_Legend.FillStyle, _Legend.Color, Color.Transparent); } if (m_ThemeType == ThemeType.Color & ShowPointLegend == true) { g.FillRectangle((Brush)BrLegend, 10, LegendTitleSize.Height + (i * LegendHeight), (int)LegendWidth / 2, LegendHeight - 2); //Half Color Box switch (_Legend.MarkerType) { case MarkerStyle.Circle: g.FillEllipse((Brush)BrLegend, 35, LegendTitleSize.Height + (i * LegendHeight) + 5, 10, 10); //Circle break; case MarkerStyle.Square: g.FillRectangle((Brush)BrLegend, 35, LegendTitleSize.Height + (i * LegendHeight) + 5, 10, 10); //Square break; case MarkerStyle.Triangle: PointF[] Vertex = new PointF[3]; //Triangle Vertex[0] = new PointF(40, LegendTitleSize.Height + (i * LegendHeight) + 5); Vertex[1] = new PointF(35, LegendTitleSize.Height + (i * LegendHeight) + 15); Vertex[2] = new PointF(45, LegendTitleSize.Height + (i * LegendHeight) + 15); g.FillPolygon((Brush)BrLegend, Vertex); break; case MarkerStyle.Cross: Pen Pn = new Pen(_Legend.Color); //Cross g.DrawLine(Pn, 35, LegendTitleSize.Height + (i * LegendHeight) + 10, 45, LegendTitleSize.Height + (i * LegendHeight) + 10); g.DrawLine(Pn, 40, LegendTitleSize.Height + (i * LegendHeight) + 5, 40, LegendTitleSize.Height + (i * LegendHeight) + 15); Pn.Dispose(); break; } } else { if (m_MultiLegend == true) { g.FillRectangle((Brush)BrLegend, 10 + (LegendWidth * MultiLegendIndex) + MultiLegendIndex, LegendTitleSize.Height + (i * LegendHeight) + 5, LegendWidth, LegendHeight - 2); //Color Box } else { //If legend postion exceeds max Legend width then reset the left position and height to wrap text if (legendSequenceOrder == LegendsSequenceOrder.SingleRow && (m_LegendMaxWidth) <= ((g.MeasureString(LegendCaption.Trim(), m_LegendBodyFont).Width) + LegendLeftPos + LegendWidth)) { LegendLeftPos = 0; LegendTitleSize.Height = LegendTitleSize.Height + ImageRowHeight; } //Color Box if (legendSequenceOrder == LegendsSequenceOrder.SingleRow) { //- Starting left position of Legend's Color box LegendLeftPos += 1; //10 di Profiles //-show color box adjacent (right) to previous Legend g.FillRectangle((Brush)BrLegend, LegendLeftPos, LegendTitleSize.Height + 5, LegendWidth, LegendHeight - 2); LegendLeftPos += LegendWidth; } else { //-show color box below previous Legend g.FillRectangle((Brush)BrLegend, 10, LegendTitleSize.Height + (i * LegendHeight) + 5, LegendWidth, LegendHeight - 2); } } } if (m_MultiLegend == true) { if (MultiLegendIndex == m_MultiLegendCol.Count - 1) { //Range Title g.DrawString(LegendCaption.Trim(), m_LegendBodyFont, BrTitle, (LegendWidth * m_MultiLegendCol.Count) + 20, LegendTitleSize.Height + 5 + (i * LegendHeight)); } } else { //Draw Range Title string if (legendSequenceOrder == LegendsSequenceOrder.SingleColumn) { g.DrawString(LegendCaption.Trim(), m_LegendBodyFont, BrTitle, LegendWidth + 20, LegendTitleSize.Height + 5 + (i * LegendHeight)); } else { //- if in single row, then draw caption next to Color Box LegendLeftPos += 1; //10 di Profiles g.DrawString(LegendCaption.Trim(), m_LegendBodyFont, BrTitle, LegendLeftPos, LegendTitleSize.Height + 5 ); //- For next Legend, increment left position by caption width. LegendLeftPos += (int)(g.MeasureString(LegendCaption.Trim(), m_LegendBodyFont).Width); } } } } //if ((BrLegend != null)) // BrLegend = null; break; } BrTitle.Dispose(); }
public Size GetLegendImage(string p_Path, string p_FileName, string p_FileExt, bool ShowPointLegend, bool showCaption, bool showRange, bool showAreaCount, bool showMissingDataInLegend, LegendsSequenceOrder legendSequenceOrder) { Size RetVal = new Size(0, 0); int ImageWidth; int ImageHeight = 0; int LegendWidth; int LegendHeight; int Padding; SizeF LegendSize; SizeF LegendTitleSize; int i = 0; this.ShowLegendRange = showRange; this.ShowLegendCaption = showCaption; this.ShowAreaCount = showAreaCount; if (m_MultiLegend == true) { LegendWidth = 20; } else { LegendWidth = 10; //40 di Profiles } LegendHeight = 5; //20 di Profiles Padding = 10; System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(1, 1); Graphics g = Graphics.FromImage(bmp); g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; LegendTitleSize = g.MeasureString(m_LegendTitle, m_LegendFont); //*** Get maximum width of Legend Items ImageWidth = (int)LegendTitleSize.Width + Padding; switch (m_ThemeType) { case ThemeType.Color: case ThemeType.Hatch: case ThemeType.Symbol: //-- calculate Legend Height (N Legend's Height) LegendHeight = (int)g.MeasureString(m_Legends[0].Title, m_LegendBodyFont).Height; if (legendSequenceOrder == LegendsSequenceOrder.SingleColumn) { //- Show all legends vertically in one column. //- Get image height by summing all legend's height ImageHeight = (m_Legends.Count - Convert.ToInt32(!(showMissingDataInLegend))) * LegendHeight + (m_MultiLegendCol.Count * LegendHeight / 2); //- Add Legend title height ImageHeight += (int)LegendTitleSize.Height + Padding; } else if (legendSequenceOrder == LegendsSequenceOrder.SingleRow) { //- Show all legends horizontally in single row. //- Get image height = Single Legend Height + LegendTitle height ImageHeight = LegendHeight + (int)LegendTitleSize.Height + Padding; } //*** one extra for legend title int ColorBlockWidth; if (m_MultiLegend == true) { ColorBlockWidth = Padding + (LegendWidth * m_MultiLegendCol.Count) + m_MultiLegendCol.Count + Padding; } else { ColorBlockWidth = Padding + LegendWidth + Padding; } //- Calculate Image width foreach (Legend _Legend in m_Legends) { i += 1; //-- Legend Title displayed should be: Legend.Caption + (Legend.Title) " string LegendCaption = string.Empty; if (this.ShowLegendCaption) { LegendCaption = _Legend.Caption + " "; } else if (showMissingDataInLegend && i == m_Legends.Count) { //- Show MissingData for Last Legend if Caption is blank LegendCaption = _Legend.Caption + " "; } if (this.ShowLegendRange && (!(string.IsNullOrEmpty(_Legend.Title)))) { LegendCaption += " " + _Legend.Title; } if (this.ShowAreaCount) { LegendCaption += " (" + Convert.ToString(_Legend.ShapeCount) + ")"; } LegendSize = g.MeasureString(LegendCaption, m_LegendBodyFont); if (legendSequenceOrder == LegendsSequenceOrder.SingleColumn) { //- if single column, then get maximum legend's width ImageWidth = Math.Max(ImageWidth, ColorBlockWidth + (int)LegendSize.Width); } else if (legendSequenceOrder == LegendsSequenceOrder.SingleRow) { if (i < m_Legends.Count || (showMissingDataInLegend && i == m_Legends.Count)) { //- if single row, then get sum of all legend's width ImageWidth += LegendWidth + Padding + (int)LegendSize.Width + Padding; } } } // In case of word wrap when single row legend exceeds map width, increase legend heigt to accomodate wraped text if (legendSequenceOrder == LegendsSequenceOrder.SingleRow && this.m_LegendMaxWidth < ImageWidth) { ImageWidth = Convert.ToInt32(this.m_LegendMaxWidth.ToString()); ImageHeight = ImageHeight + ImageHeight; // } break; case ThemeType.Label: int LegendHeightSum = 0; //-- Get sum of all Legends hights // as in label theme, each label is set with its individual font. foreach (Legend _Legend in m_Legends) { LegendHeightSum += (int)g.MeasureString(_Legend.Title, _Legend.MarkerFont).Height; } LegendHeight = LegendHeightSum / m_Legends.Count; ImageHeight = LegendHeightSum + (m_MultiLegendCol.Count * LegendHeight / 2); ImageHeight += (int)LegendTitleSize.Height + Padding; //*** one extra for legend title if (m_MultiLegend == true) { ColorBlockWidth = Padding + (LegendWidth * m_MultiLegendCol.Count) + m_MultiLegendCol.Count + Padding; } else { ColorBlockWidth = Padding + LegendWidth + Padding; } foreach (Legend _Legend in m_Legends) { i += 1; //-- Legend Title displayed should be: Legend.Caption + (Legend.Title) " string LegendCaption = string.Empty; if (this.ShowLegendCaption) { LegendCaption = _Legend.Caption + " "; } else if (showMissingDataInLegend && i == m_Legends.Count) { //- Show MissingData for Last Legend if Caption is blank LegendCaption = _Legend.Caption + " "; } if (this.ShowLegendRange && (!(string.IsNullOrEmpty(_Legend.Title)))) { LegendCaption += " " + _Legend.Title; } if (this.ShowAreaCount) { LegendCaption += " (" + Convert.ToString(_Legend.ShapeCount) + ")"; } LegendSize = g.MeasureString(LegendCaption, _Legend.MarkerFont); ImageWidth = Math.Max(ImageWidth, ColorBlockWidth + (int)LegendSize.Width); } break; case ThemeType.DotDensity: LegendSize = g.MeasureString(m_DotValue + " " + m_UnitName, m_LegendBodyFont); ImageWidth = Math.Max((int)LegendTitleSize.Width + Padding, 20 + (int)LegendSize.Width); ImageHeight = Math.Min(2 * (int)LegendTitleSize.Height, (int)LegendTitleSize.Height + (int)LegendSize.Height); break; case ThemeType.Chart: //get height of any one legend item based on legend font LegendHeight = Math.Max(LegendHeight, (int)g.MeasureString(SubgroupName[0].ToString(), m_LegendBodyFont).Height); if (legendSequenceOrder == LegendsSequenceOrder.SingleColumn) { //- Show all legends vertically in one column. //ImageHeight = (SubgroupName.Length + 1) * LegendHeight; // Set Image height of based on number of legend Item ImageHeight = (SubgroupName.Length) * LegendHeight; //- Add Legend title height to image height ImageHeight += (int)(g.MeasureString(IndicatorName[0].ToString(), m_LegendBodyFont).Height)+Padding; } else if (legendSequenceOrder == LegendsSequenceOrder.SingleRow) { //- Show all legends horizontally in single row. ImageHeight = LegendHeight; } //ImageHeight += (int)LegendTitleSize.Height + Padding; //*** one extra for legend title foreach (string _Str in IndicatorName) { LegendSize = g.MeasureString(_Str, m_LegendBodyFont); ImageWidth = Math.Max(ImageWidth, Padding + LegendWidth + Padding + (int)LegendSize.Width); } //- Calculate image width depending upon following cases //- If all legends to be shown in single column. // or all legends in single row. if (legendSequenceOrder == LegendsSequenceOrder.SingleColumn) { //- if single column, then get maximum subgroup's width foreach (string _Str in SubgroupName) { LegendSize = g.MeasureString(_Str, m_LegendBodyFont); ImageWidth = Math.Max(ImageWidth, Padding + LegendWidth + Padding + (int)LegendSize.Width); } } else if (legendSequenceOrder == LegendsSequenceOrder.SingleRow) { int SubgroupCaptionWidth = 0; foreach (string _Str in SubgroupName) { LegendSize = g.MeasureString(_Str, m_LegendBodyFont); SubgroupCaptionWidth += Padding + LegendWidth + Padding + (int)LegendSize.Width; } ImageWidth = Math.Max(ImageWidth, SubgroupCaptionWidth); } //-- Now consider Legend Title width ImageWidth = Math.Max((int)LegendTitleSize.Width + Padding, ImageWidth); break; } bmp = new System.Drawing.Bitmap(ImageWidth + 2, ImageHeight); g = Graphics.FromImage(bmp); switch (p_FileExt.ToLower()) { case "emf": IntPtr hRefDC = g.GetHdc(); //Metafile m = new Metafile(p_Path + "\\" + p_FileName + ".emf", hRefDC); Metafile m = new Metafile(p_Path + "\\" + p_FileName + ".emf", hRefDC, new Rectangle(0, 0, ImageWidth + 2, ImageHeight), MetafileFrameUnit.Pixel, EmfType.EmfPlusOnly); g.ReleaseHdc(hRefDC); Graphics gMeta = Graphics.FromImage(m); DrawLegend(ref gMeta, LegendWidth, LegendHeight, ShowPointLegend, showMissingDataInLegend, legendSequenceOrder); m.Dispose(); gMeta.Dispose(); break; case "png": g.Clear(Color.White); DrawLegend(ref g, LegendWidth, LegendHeight, ShowPointLegend, showMissingDataInLegend, legendSequenceOrder); bmp.Save(p_Path + "\\" + p_FileName + ".png", ImageFormat.Png); break; case "jpg": g.Clear(Color.White); DrawLegend(ref g, LegendWidth, LegendHeight, ShowPointLegend, showMissingDataInLegend, legendSequenceOrder); //bmp.Save(p_Path + "\\" + p_FileName + ".jpg", ImageFormat.Jpeg); Map.SaveAsHightQualityJPEG( bmp, p_Path + "\\" + p_FileName + ".jpg"); break; case "bmp": g.Clear(Color.White); DrawLegend(ref g, LegendWidth, LegendHeight, ShowPointLegend, showMissingDataInLegend, legendSequenceOrder); bmp.Save(p_Path + "\\" + p_FileName + ".bmp", ImageFormat.Bmp); break; case "gif": g.Clear(Color.White); DrawLegend(ref g, LegendWidth, LegendHeight, ShowPointLegend, showMissingDataInLegend, legendSequenceOrder); bmp.Save(p_Path + "\\" + p_FileName + ".gif", ImageFormat.Gif); break; case "tiff": DrawLegend(ref g, LegendWidth, LegendHeight, ShowPointLegend, showMissingDataInLegend, legendSequenceOrder); bmp.Save(p_Path + "\\" + p_FileName + ".tiff", ImageFormat.Tiff); break; case "ico": DrawLegend(ref g, LegendWidth, LegendHeight, ShowPointLegend, showMissingDataInLegend, legendSequenceOrder); bmp.Save(p_Path + "\\" + p_FileName + ".ico", ImageFormat.Icon); break; } RetVal.Height = ImageHeight; RetVal.Width = ImageWidth; bmp.Dispose(); bmp = null; g.Dispose(); return RetVal; }
private void DrawLegend(ref Graphics g, int LegendWidth, int LegendHeight, bool ShowPointLegend, bool showMissingDataInLegend, LegendsSequenceOrder legendSequenceOrder) { //*** Legend Title SolidBrush BrTitle = new SolidBrush(m_LegendColor); SolidBrush BrLegendBody = new SolidBrush(m_LegendBodyColor); g.SmoothingMode = SmoothingMode.AntiAlias; g.DrawString(m_LegendTitle + " ", m_LegendFont, BrTitle, 10, 0); int LegendTitleHeight = (int)g.MeasureString(m_LegendTitle, m_LegendFont).Height; //*** Legend Body switch (m_ThemeType) { case ThemeType.Color: case ThemeType.Hatch: case ThemeType.Symbol: case ThemeType.Label: //*** In case of multilegend draw element name (source/subgroup/metadata) at the bottom of legend if (m_MultiLegend == true) { SolidBrush BrFooter = new SolidBrush(m_LegendColor); Font FntFooter = new Font("Arial", 6); IDictionaryEnumerator MultiLegendEnumerator = m_MultiLegendCol.GetEnumerator(); int MultiLegendIndex = 0; while (MultiLegendEnumerator.MoveNext()) { DrawMultiLegend(ref g, (Legends)MultiLegendEnumerator.Value, LegendWidth, LegendHeight, ShowPointLegend, showMissingDataInLegend, MultiLegendIndex, legendSequenceOrder); //*** Draw Source/Subgroup/Metadata Text as Footer g.DrawString(MultiLegendEnumerator.Key.ToString(), FntFooter, BrFooter, 10, ((((Legends)MultiLegendEnumerator.Value).Count + 1) * LegendHeight) + (MultiLegendIndex * LegendHeight / 2) + 2); //Range Title MultiLegendIndex += 1; } BrFooter.Dispose(); FntFooter.Dispose(); } else { DrawMultiLegend(ref g, m_Legends, LegendWidth, LegendHeight, ShowPointLegend, showMissingDataInLegend, -1, legendSequenceOrder); } break; case ThemeType.DotDensity: Font fnt = new Font("Webdings", m_DotSize, FontStyle.Regular); SolidBrush BrDot = new SolidBrush(m_DotColor); switch (m_DotStyle) { case MarkerStyle.Circle: g.FillEllipse(BrDot, new RectangleF(10 - (m_DotSize / 2), (float)(1.5 * LegendTitleHeight) - m_DotSize, m_DotSize, m_DotSize)); break; case MarkerStyle.Square: g.FillRectangle(BrDot, new RectangleF(10 - (m_DotSize / 2), (float)(1.5 * LegendTitleHeight) - m_DotSize, m_DotSize, m_DotSize)); break; case MarkerStyle.Triangle: g.DrawString("5", fnt, BrDot, 10, LegendTitleHeight); //Range Title break; case MarkerStyle.Cross: fnt = new Font("Arial", m_DotSize, FontStyle.Regular); g.DrawString("+", fnt, BrDot, 10, LegendTitleHeight); //Range Title break; case MarkerStyle.Custom: g.DrawString(m_DotChar.ToString(), m_DotFont, BrDot, 10, LegendTitleHeight); //Range Title break; } g.DrawString(m_DotValue + " " + m_UnitName, m_LegendBodyFont, BrLegendBody, 20, LegendTitleHeight); //Range Title fnt.Dispose(); BrDot.Dispose(); break; case ThemeType.Chart: int k = 0; string[] VisibleSeries = null; // Either Source, Or SubgroupVal string[] ChartSeriesName = null; Brush BrLegend; int LegendLeftPos = 0; if (this._ChartSeriesType == ChartSeriesType.Source) { VisibleSeries = this._SourceVisible; ChartSeriesName = this._SourceName; } else if (this._ChartSeriesType == ChartSeriesType.Subgroup) { VisibleSeries = this.SubgroupVisible; ChartSeriesName = this.SubgroupName; } for (int i = 0; i <= ChartSeriesName.Length - 1; i++) { LegendHeight = Math.Max(LegendHeight, (int)g.MeasureString(ChartSeriesName[i].ToString(), m_LegendBodyFont).Height); //*** Bugfix Feb 06 Show only those chart legend which are selected at step1 if (VisibleSeries[i] == "1") { BrLegend = new SolidBrush(Color.FromArgb(int.Parse(SubgroupFillStyle[i]))); if (legendSequenceOrder == LegendsSequenceOrder.SingleRow) { //- Starting left position of Legend's Color box LegendLeftPos += 10; //-show color box adjacent (right) to previous Legend g.FillRectangle(BrLegend, LegendLeftPos, LegendTitleHeight, LegendWidth, LegendHeight - 2); //-increment left position for cation to show LegendLeftPos += LegendWidth; g.DrawString(ChartSeriesName[i], m_LegendBodyFont, BrLegendBody, LegendLeftPos+5, LegendTitleHeight + 5); //- For next Legend, increment left position by caption width. LegendLeftPos += (int)(g.MeasureString(ChartSeriesName[i].Trim(), m_LegendBodyFont).Width)+ 5; } else { g.FillRectangle(BrLegend, 10, LegendTitleHeight + (k * LegendHeight)+5, LegendWidth, LegendHeight - 2); g.DrawString(ChartSeriesName[i], m_LegendBodyFont, BrLegendBody, LegendWidth + 20, LegendTitleHeight + (k * LegendHeight)+5); ////////Color Box ////////To Do Change //g.DrawString(ChartSeriesName[i], m_LegendBodyFont, BrLegendBody, LegendWidth + 20, LegendTitleHeight + 5 + (k * LegendHeight)); ////////Change //////g.DrawString(ChartSeriesName[i], m_LegendBodyFont, BrLegendBody, LegendTitleHeight + LegendHeight + 5, LegendTitleHeight + (k * LegendHeight)); ////////Change end } //Range Title BrLegend.Dispose(); k += 1; } } //if ((BrLegend != null)) // BrLegend.Dispose(); break; } BrTitle.Dispose(); BrLegendBody.Dispose(); }
/// <summary> /// Get composite legend image /// </summary> /// <param name="outputFilePath"></param> /// <param name="fileName"></param> /// <param name="p_FileExt"></param> /// <param name="tempFolder"></param> /// <param name="legendsSequenceOrder"></param> /// <param name="compositeLegendImageOrder"></param> public void GetCompositeLegendImage(string outputPath, string fileName, string p_FileExt, string tempFolder, LegendsSequenceOrder legendsSequenceOrder, CompositeLegendImageOrder compositeLegendImageOrder) { string FilePostfix = DateTime.Now.Ticks.ToString(); string LegendImgFile = string.Empty; //string CompositeLegendImage = "Legend" + FilePostfix; Image[] LegendImages = null; Size LegendImgSize = new Size(); Graphics g = null; Metafile mFile = null; System.Drawing.Bitmap bmp = null; try { if (this.m_Themes.Count > 0) { LegendImages = new Image[this.m_Themes.Count]; //- Get Legend Images in Image Array for (int i = 0; i < this.m_Themes.Count; i++) { Theme theme = this.m_Themes[i]; if (theme.Visible) { LegendImgFile = "Legend_" + i + FilePostfix; //set legend Maximum width according to map width theme.LegendMaxWidth = this.m_Width - 2; theme.GetLegendImage(tempFolder, LegendImgFile, p_FileExt, false, this._TemplateStyle.Legends.ShowCaption, this._TemplateStyle.Legends.ShowRange, this._TemplateStyle.Legends.ShowCount, this._TemplateStyle.Legends.ShowMissingLegend, legendsSequenceOrder); LegendImages[i] = Image.FromFile(Path.Combine(tempFolder, LegendImgFile + "." + p_FileExt)); } } //- Start adding Legend Images in a single composite Legend image. foreach (object img in LegendImages) { //- Calculate composite Legend Image Size if (img != null) { if (compositeLegendImageOrder == CompositeLegendImageOrder.Horizontal) { //- if all legend Image are horizontally positioned LegendImgSize.Width += ((Image)img).Width; LegendImgSize.Height = Math.Max(LegendImgSize.Height, ((Image)img).Height); } else { //- if all legend Images are vertically positioned LegendImgSize.Width = Math.Max(LegendImgSize.Width, ((Image)img).Width); LegendImgSize.Height += ((Image)img).Height; } } } //- Get new graphics object bmp = new System.Drawing.Bitmap(LegendImgSize.Width, LegendImgSize.Height); g = Graphics.FromImage(bmp); g.Clear(Color.White); if (p_FileExt.ToLower() == "emf") { IntPtr hRefDC = g.GetHdc(); //mFile = new Metafile(outputPath + "\\" + fileName + "." + p_FileExt, hRefDC, new Rectangle(0, 0, LegendImgSize.Width, LegendImgSize.Height, MetafileFrameUnit.Pixel, EmfType.EmfPlusDual)); //MetafileFrameUnit.Point g.Dispose(); g = Graphics.FromImage(mFile); } int leftPos = 0; int topPos = 0; foreach (object img in LegendImages) { if (img != null) { g.DrawImage(((Image)img), leftPos, topPos); if (compositeLegendImageOrder == CompositeLegendImageOrder.Horizontal) { //- if all legend Image are horizontally positioned leftPos += ((Image)img).Width; } else { //- if all legend Images are vertically positioned topPos += ((Image)img).Height; } } } switch (p_FileExt.ToLower()) { case "emf": mFile.Dispose(); break; case "png": bmp.Save(outputPath + "\\" + fileName + ".png", ImageFormat.Png); break; case "jpg": //bmp.Save(p_Path + "\\" + p_FileName + ".jpg", ImageFormat.Jpeg); Map.SaveAsHightQualityJPEG(bmp, outputPath + "\\" + fileName + ".jpg"); break; case "bmp": bmp.Save(outputPath + "\\" + fileName + ".bmp", ImageFormat.Bmp); break; case "gif": bmp.Save(outputPath + "\\" + fileName + ".gif", ImageFormat.Gif); break; case "tiff": bmp.Save(outputPath + "\\" + fileName + ".tiff", ImageFormat.Tiff); break; case "ico": bmp.Save(outputPath + "\\" + fileName + ".ico", ImageFormat.Icon); break; } } } catch { } finally { if (bmp != null) { bmp.Dispose(); bmp = null; } if (g != null) { g.Dispose(); } foreach (Image img in LegendImages) { if (img != null) { img.Dispose(); } } } }