/// <summary> /// Copy constructor; instantiates a new ChartProperties object /// whose values are copied from an existing ChartProperties object. /// </summary> /// <param name="copy"> /// The <c>ChartProperties</c> object whose values are to be copied. /// </param> public ChartProperties(ChartProperties copy) { fontSet.Add("titleFont", copy.fontSet["titleFont"]); fontSet.Add("axisFont", copy.fontSet["axisFont"]); fontSet.Add("labelFont", copy.fontSet["labelFont"]); fontSet.Add("valueFont", copy.fontSet["valueFont"]); fontSet.Add("legendFont", copy.fontSet["legendFont"]); LineWeight = copy.LineWeight; MarkerSize = copy.MarkerSize; dock = copy.dock; resolution.Add("width", copy.resolution["width"]); resolution.Add("height", copy.resolution["height"]); }
private void RightClickSaveImage(object sender, MouseEventArgs e) { if ((e.Button == System.Windows.Forms.MouseButtons.Right) && (sender != null)) { if (SaveImageDialog.ShowDialog() == DialogResult.OK) { if (sender is PictureBox) { PictureBox pb = sender as PictureBox; Bitmap output = new Bitmap(pb.Image); output.Save(SaveImageDialog.FileName, System.Drawing.Imaging.ImageFormat.Png); } else if (sender is System.Windows.Forms.DataVisualization.Charting.Chart) { System.Windows.Forms.DataVisualization.Charting.Chart chart = sender as System.Windows.Forms.DataVisualization.Charting.Chart; ChartProperties chartprops = new ChartProperties(); chartprops.ExportChartAsPNG(chart, SaveImageDialog.FileName, 800, 800); } } } }
public void ExportChartAsPNG( Chart input, string path, int imageWidth, int imageHeight) { // The size of font elements. // Assumes 150 dpi, 72 dpi font specification, // and arbitrary font scaling ratio (s) // [e.g. titles larger than lables]. // // font_size = [72/150] * s * imageHeight // s = (1/20) => 0.024 // s = (1/24) => 0.02 float titlePt = (float)Math.Floor(0.024 * (double)imageHeight); float axisPt = (float)Math.Floor(0.02 * (double)imageHeight); float valuePt = (float)Math.Floor(0.02 * (double)imageHeight); float labelPt = (float)Math.Floor(0.02 * (double)imageHeight); float legendPt = (float)Math.Floor(0.02 * (double)imageHeight); if ((int)titlePt % 2 != 0) { titlePt += 1.0F; } if ((int)axisPt % 2 != 0) { axisPt += 1.0F; } if ((int)valuePt % 2 != 0) { valuePt += 1.0F; } if ((int)labelPt % 2 != 0) { labelPt += 1.0F; } if ((int)legendPt % 2 != 0) { legendPt += 1.0F; } // Line weights correspond to: // lineWeight = 2 @ imageHeight = 480px // lineWeight = 5 @ imageHeight = 1080px int lineWeight = (int)(((1.0 / 200.0) * (double)imageHeight) - 0.4); if (lineWeight < 1) { lineWeight = 1; } int markerSize = (int)Math.Round((((1.0 / 200.0) * (double)imageHeight) + 3.6)); if (markerSize < 5) { markerSize = 5; } // Create scaled fonts for the output image plot. Font valueFont; Font legendFont; Font titleFont = new Font(input.Titles[0].Font.Name, titlePt, FontStyle.Bold); Font axisFont = new Font(input.ChartAreas[0].Axes[0].TitleFont.Name, axisPt, FontStyle.Regular); Font labelFont = new Font(input.ChartAreas[0].Axes[0]. LabelStyle.Font.Name, labelPt, FontStyle.Regular); if (input.Series.Count > 0) { valueFont = new Font(input.Series[0].Font.Name, valuePt, FontStyle.Regular); } else { valueFont = new Font(FontFamily.GenericSansSerif, valuePt, FontStyle.Regular); } if (input.Legends.Count > 0) { legendFont = new Font(input.Legends[0].Font.Name, legendPt, FontStyle.Regular); } else { legendFont = new Font(FontFamily.GenericSansSerif, legendPt, FontStyle.Regular); } ChartProperties imageProps = new ChartProperties(); imageProps.Modify(titleFont, axisFont, labelFont, valueFont, legendFont, lineWeight, markerSize, imageWidth, imageHeight, System.Windows.Forms.DockStyle.None); Chart temp = imageProps.Clone(input); imageProps.RestoreProperties(ref temp); temp.SaveImage(path, ChartImageFormat.Png); }