Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/07/11 11:38:44 (13 years ago)
Author:
abeham
Message:

#1611

  • allow to specify font size of legend items
  • cleaned up code a little (dpi/dpcm, cm/inch)
  • added detection for region settings to select either cm or inch
  • improved preview view (now doesn't scale images beyond 100%)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Visualization.ChartControlsExtensions/3.3/ImageExportDialog.cs

    r6640 r6641  
    2929
    3030namespace HeuristicLab.Visualization.ChartControlsExtensions {
    31   public enum LengthUnit { Centimeters = 1, Inches = 2 };
    32 
    3331  public sealed partial class ImageExportDialog : Form {
    3432    private const float CMPERINCH = 2.54f;
     33    private static readonly string DPI = "dpi", DPCM = "dpcm", INCH = "inch", CM = "cm";
    3534    private Chart originalChart, workingChart;
    3635    private bool SuppressEvents { get; set; }
     
    4746      this.originalChart = chart;
    4847      InitializeComponent();
     48      #region Custom Initialization
    4949      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
    5871    }
    5972
     
    6477        SuppressEvents = true;
    6578
    66         if (workingChart.Titles.Count == 0) titleComboBox.Text = "12";
     79        if (workingChart.Titles.Count == 0) titleFontSizeComboBox.Text = "12";
    6780        else {
    6881          titleTextBox.Text = workingChart.Titles[0].Text;
    69           titleComboBox.Text = workingChart.Titles[0].Font.SizeInPoints.ToString();
     82          titleFontSizeComboBox.Text = workingChart.Titles[0].Font.SizeInPoints.ToString();
    7083        }
    7184
     
    7588        secondaryYTextBox.Text = area.AxisY2.Title;
    7689
    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();
    7994      } finally {
    8095        SuppressEvents = false;
     
    87102
    88103    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
    98114      int previewWidth, previewHeight;
    99115      if (width / height >= 1.0) {
     
    104120        previewWidth = (int)Math.Round(width / height * previewHeight);
    105121      }
     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
    106130      Bitmap image = new Bitmap(previewWidth, previewHeight);
    107131      image.SetResolution(dpi, dpi);
    108132      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)));
    116135      }
    117136      previewPictureBox.Image = image;
    118137    }
    119138
     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
    120150    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;
    122154      originalChart.Serializer.Content = SerializationContents.Default;
     155      originalChart.Serializer.Format = SerializationFormat.Binary;
    123156      MemoryStream ms = new MemoryStream();
    124157      originalChart.Serializer.Save(ms);
    125       originalChart.Serializer.Content = prev;
     158      originalChart.Serializer.Content = prevContent;
     159      originalChart.Serializer.Format = prevFormat;
    126160
    127161      ms.Seek(0, SeekOrigin.Begin);
     
    129163      workingChart.Serializer.Load(ms);
    130164      ms.Close();
     165      #endregion
    131166
    132167      chartAreaComboBox.Items.Clear();
     
    161196        } else {
    162197          Title t = new Title(titleTextBox.Text);
     198          t.Font = ChangeFontSizePt(t.Font, float.Parse(titleFontSizeComboBox.Text));
    163199          workingChart.Titles.Add(t);
    164200        }
     
    207243    }
    208244
    209     private void titleComboBox_TextChanged(object sender, EventArgs e) {
     245    private void titleFontSizeComboBox_TextChanged(object sender, EventArgs e) {
    210246      if (!SuppressEvents) {
    211247        float fontSize;
    212         if (float.TryParse(titleComboBox.Text, out fontSize)) {
     248        if (float.TryParse(titleFontSizeComboBox.Text, out fontSize)) {
    213249          if (workingChart.Titles.Count > 0) {
    214250            workingChart.Titles[0].Font = ChangeFontSizePt(workingChart.Titles[0].Font, fontSize);
     
    219255    }
    220256
    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) {
    227258      if (!SuppressEvents) {
    228259        float fontSize;
    229         if (float.TryParse(axisComboBox.Text, out fontSize)) {
     260        if (float.TryParse(axisFontSizeComboBox.Text, out fontSize)) {
    230261          ChartArea area = GetCurrentChartArea();
    231262          foreach (Axis a in area.Axes) {
     
    237268    }
    238269
    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) {
    245271      if (!SuppressEvents) {
    246272        float fontSize;
    247         if (float.TryParse(scalesComboBox.Text, out fontSize)) {
     273        if (float.TryParse(scalesFontSizeComboBox.Text, out fontSize)) {
    248274          ChartArea area = GetCurrentChartArea();
    249275          foreach (Axis a in area.Axes) {
     
    255281    }
    256282
    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;
    258297      float number;
    259       e.Cancel = !float.TryParse(scalesComboBox.Text, out number);
     298      e.Cancel = !float.TryParse((sender as ComboBox).Text, out number);
    260299    }
    261300
     
    281320
    282321    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));
    292328      image.SetResolution(dpi, dpi);
    293329      using (Graphics graphics = Graphics.FromImage(image)) {
     
    295331      }
    296332
     333      if (titleTextBox.Text.Trim() != String.Empty) saveFileDialog.FileName = titleTextBox.Text.Trim();
    297334      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
    298335        ImageFormat format = ImageFormat.Bmp;
Note: See TracChangeset for help on using the changeset viewer.