Changeset 6641 for trunk/sources/HeuristicLab.Visualization.ChartControlsExtensions/3.3/ImageExportDialog.cs
- Timestamp:
- 08/07/11 11:38:44 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Visualization.ChartControlsExtensions/3.3/ImageExportDialog.cs
r6640 r6641 29 29 30 30 namespace HeuristicLab.Visualization.ChartControlsExtensions { 31 public enum LengthUnit { Centimeters = 1, Inches = 2 };32 33 31 public sealed partial class ImageExportDialog : Form { 34 32 private const float CMPERINCH = 2.54f; 33 private static readonly string DPI = "dpi", DPCM = "dpcm", INCH = "inch", CM = "cm"; 35 34 private Chart originalChart, workingChart; 36 35 private bool SuppressEvents { get; set; } … … 47 46 this.originalChart = chart; 48 47 InitializeComponent(); 48 #region Custom Initialization 49 49 SuppressEvents = true; 50 titleComboBox.Text = "12"; 51 resolutionUnitComboBox.SelectedIndex = 0; 52 lengthUnitComboBox.SelectedIndex = 1; 53 resolutionComboBox.Text = "300"; 54 SuppressEvents = false; 55 splitContainer.Panel2Collapsed = true; 56 Width = 305; 57 Height = 550; 50 try { 51 resolutionUnitComboBox.Items.Add(DPI); 52 resolutionUnitComboBox.Items.Add(DPCM); 53 lengthUnitComboBox.Items.Add(INCH); 54 lengthUnitComboBox.Items.Add(CM); 55 resolutionUnitComboBox.SelectedIndex = 0; 56 if (System.Globalization.RegionInfo.CurrentRegion.IsMetric) 57 lengthUnitComboBox.SelectedIndex = 1; 58 else lengthUnitComboBox.SelectedIndex = 0; 59 60 titleFontSizeComboBox.Text = "12"; 61 axisFontSizeComboBox.Text = "8"; 62 scalesFontSizeComboBox.Text = "6"; 63 legendFontSizeComboBox.Text = "6"; 64 resolutionComboBox.Text = "300"; 65 SuppressEvents = false; 66 splitContainer.Panel2Collapsed = true; 67 Width = 305; 68 Height = 550; 69 } finally { SuppressEvents = false; } 70 #endregion 58 71 } 59 72 … … 64 77 SuppressEvents = true; 65 78 66 if (workingChart.Titles.Count == 0) title ComboBox.Text = "12";79 if (workingChart.Titles.Count == 0) titleFontSizeComboBox.Text = "12"; 67 80 else { 68 81 titleTextBox.Text = workingChart.Titles[0].Text; 69 title ComboBox.Text = workingChart.Titles[0].Font.SizeInPoints.ToString();82 titleFontSizeComboBox.Text = workingChart.Titles[0].Font.SizeInPoints.ToString(); 70 83 } 71 84 … … 75 88 secondaryYTextBox.Text = area.AxisY2.Title; 76 89 77 axisComboBox.Text = area.AxisX.TitleFont.SizeInPoints.ToString(); 78 scalesComboBox.Text = area.AxisX.LabelStyle.Font.SizeInPoints.ToString(); 90 axisFontSizeComboBox.Text = area.AxisX.TitleFont.SizeInPoints.ToString(); 91 scalesFontSizeComboBox.Text = area.AxisX.LabelStyle.Font.SizeInPoints.ToString(); 92 if (workingChart.Legends.Count == 0) legendFontSizeComboBox.Text = "8"; 93 else legendFontSizeComboBox.Text = workingChart.Legends[0].Font.SizeInPoints.ToString(); 79 94 } finally { 80 95 SuppressEvents = false; … … 87 102 88 103 private void UpdatePreview() { 89 float dpi = float.Parse(resolutionComboBox.Text); 90 if (resolutionUnitComboBox.SelectedIndex == 1) dpi *= CMPERINCH; 91 float width = (float)widthNumericUD.Value; 92 float height = (float)heightNumericUD.Value; 93 if (lengthUnitComboBox.SelectedIndex == 1) { 94 width /= CMPERINCH; height /= CMPERINCH; 95 } 96 width *= dpi; height *= dpi; 97 if (previewPictureBox.Image != null) previewPictureBox.Image.Dispose(); 104 float dpi; 105 float width; 106 float height; 107 GetImageParameters(out dpi, out width, out height); 108 109 if (previewPictureBox.Image != null) { 110 previewPictureBox.Image.Dispose(); 111 previewPictureBox.Image = null; 112 } 113 98 114 int previewWidth, previewHeight; 99 115 if (width / height >= 1.0) { … … 104 120 previewWidth = (int)Math.Round(width / height * previewHeight); 105 121 } 122 123 float scaleFactor = (float)Math.Min(previewWidth / width, previewHeight / height); 124 if (scaleFactor >= 1) { 125 previewZoomLabel.Text = "100%"; 126 previewWidth = (int)Math.Round(width); 127 previewHeight = (int)Math.Round(height); 128 } else previewZoomLabel.Text = (scaleFactor * 100).ToString("0") + "%"; 129 106 130 Bitmap image = new Bitmap(previewWidth, previewHeight); 107 131 image.SetResolution(dpi, dpi); 108 132 using (Graphics graphics = Graphics.FromImage(image)) { 109 graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 110 graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 111 graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 112 float scaleFactor = (float)Math.Min(image.Width / width, image.Height / height); 113 previewZoomLabel.Text = (scaleFactor * 100).ToString("0") + "%"; 114 graphics.ScaleTransform(scaleFactor, scaleFactor); 115 workingChart.Printing.PrintPaint(graphics, new Rectangle(0, 0, (int)width, (int)height)); 133 if (scaleFactor < 1) graphics.ScaleTransform(scaleFactor, scaleFactor); 134 workingChart.Printing.PrintPaint(graphics, new Rectangle(0, 0, (int)Math.Round(width), (int)Math.Round(height))); 116 135 } 117 136 previewPictureBox.Image = image; 118 137 } 119 138 139 private void GetImageParameters(out float dpi, out float width, out float height) { 140 dpi = float.Parse(resolutionComboBox.Text); 141 if (resolutionUnitComboBox.Text == DPCM) dpi *= CMPERINCH; 142 width = (float)widthNumericUD.Value; 143 height = (float)heightNumericUD.Value; 144 if (lengthUnitComboBox.Text == CM) { 145 width /= CMPERINCH; height /= CMPERINCH; 146 } 147 width *= dpi; height *= dpi; 148 } 149 120 150 protected override void OnShown(EventArgs e) { 121 var prev = originalChart.Serializer.Content; 151 #region Create copy of chart 152 var prevContent = originalChart.Serializer.Content; 153 var prevFormat = originalChart.Serializer.Format; 122 154 originalChart.Serializer.Content = SerializationContents.Default; 155 originalChart.Serializer.Format = SerializationFormat.Binary; 123 156 MemoryStream ms = new MemoryStream(); 124 157 originalChart.Serializer.Save(ms); 125 originalChart.Serializer.Content = prev; 158 originalChart.Serializer.Content = prevContent; 159 originalChart.Serializer.Format = prevFormat; 126 160 127 161 ms.Seek(0, SeekOrigin.Begin); … … 129 163 workingChart.Serializer.Load(ms); 130 164 ms.Close(); 165 #endregion 131 166 132 167 chartAreaComboBox.Items.Clear(); … … 161 196 } else { 162 197 Title t = new Title(titleTextBox.Text); 198 t.Font = ChangeFontSizePt(t.Font, float.Parse(titleFontSizeComboBox.Text)); 163 199 workingChart.Titles.Add(t); 164 200 } … … 207 243 } 208 244 209 private void title ComboBox_TextChanged(object sender, EventArgs e) {245 private void titleFontSizeComboBox_TextChanged(object sender, EventArgs e) { 210 246 if (!SuppressEvents) { 211 247 float fontSize; 212 if (float.TryParse(title ComboBox.Text, out fontSize)) {248 if (float.TryParse(titleFontSizeComboBox.Text, out fontSize)) { 213 249 if (workingChart.Titles.Count > 0) { 214 250 workingChart.Titles[0].Font = ChangeFontSizePt(workingChart.Titles[0].Font, fontSize); … … 219 255 } 220 256 221 private void titleComboBox_Validating(object sender, CancelEventArgs e) { 222 float number; 223 e.Cancel = !float.TryParse(titleComboBox.Text, out number); 224 } 225 226 private void axisComboBox_TextChanged(object sender, EventArgs e) { 257 private void axisFontSizeComboBox_TextChanged(object sender, EventArgs e) { 227 258 if (!SuppressEvents) { 228 259 float fontSize; 229 if (float.TryParse(axis ComboBox.Text, out fontSize)) {260 if (float.TryParse(axisFontSizeComboBox.Text, out fontSize)) { 230 261 ChartArea area = GetCurrentChartArea(); 231 262 foreach (Axis a in area.Axes) { … … 237 268 } 238 269 239 private void axisComboBox_Validating(object sender, CancelEventArgs e) { 240 float number; 241 e.Cancel = !float.TryParse(axisComboBox.Text, out number); 242 } 243 244 private void scalesComboBox_TextChanged(object sender, EventArgs e) { 270 private void scalesFontSizeComboBox_TextChanged(object sender, EventArgs e) { 245 271 if (!SuppressEvents) { 246 272 float fontSize; 247 if (float.TryParse(scales ComboBox.Text, out fontSize)) {273 if (float.TryParse(scalesFontSizeComboBox.Text, out fontSize)) { 248 274 ChartArea area = GetCurrentChartArea(); 249 275 foreach (Axis a in area.Axes) { … … 255 281 } 256 282 257 private void scalesComboBox_Validating(object sender, CancelEventArgs e) { 283 private void legendFontSizeComboBox_TextChanged(object sender, EventArgs e) { 284 if (!SuppressEvents) { 285 float fontSize; 286 if (float.TryParse(legendFontSizeComboBox.Text, out fontSize)) { 287 foreach (Legend l in workingChart.Legends) { 288 l.Font = ChangeFontSizePt(l.Font, fontSize); 289 } 290 } 291 if (togglePreviewCheckBox.Checked) UpdatePreview(); 292 } 293 } 294 295 private void numericComboBox_Validating(object sender, CancelEventArgs e) { 296 if (!(sender is ComboBox)) return; 258 297 float number; 259 e.Cancel = !float.TryParse( scalesComboBox.Text, out number);298 e.Cancel = !float.TryParse((sender as ComboBox).Text, out number); 260 299 } 261 300 … … 281 320 282 321 private void okButton_Click(object sender, EventArgs e) { 283 float dpi = float.Parse(resolutionComboBox.Text); 284 if (resolutionUnitComboBox.SelectedIndex == 1) dpi *= CMPERINCH; 285 float width = (float)widthNumericUD.Value; 286 float height = (float)heightNumericUD.Value; 287 if (lengthUnitComboBox.SelectedIndex == 1) { 288 width /= CMPERINCH; 289 height /= CMPERINCH; 290 } 291 Bitmap image = new Bitmap((int)Math.Round(width * dpi), (int)Math.Round(height * dpi)); 322 float dpi; 323 float width; 324 float height; 325 GetImageParameters(out dpi, out width, out height); 326 327 Bitmap image = new Bitmap((int)Math.Round(width), (int)Math.Round(height)); 292 328 image.SetResolution(dpi, dpi); 293 329 using (Graphics graphics = Graphics.FromImage(image)) { … … 295 331 } 296 332 333 if (titleTextBox.Text.Trim() != String.Empty) saveFileDialog.FileName = titleTextBox.Text.Trim(); 297 334 if (saveFileDialog.ShowDialog() == DialogResult.OK) { 298 335 ImageFormat format = ImageFormat.Bmp;
Note: See TracChangeset
for help on using the changeset viewer.