Added maintain aspect ratio to image resize dialog

This commit is contained in:
Jaex 2017-10-22 03:33:30 +03:00
parent 8dbbcad272
commit 757e4b0e1d
2 changed files with 55 additions and 10 deletions

View file

@ -51,7 +51,6 @@ private void InitializeComponent()
0,
0});
this.nudWidth.Name = "nudWidth";
this.nudWidth.ValueChanged += new System.EventHandler(this.nudWidth_ValueChanged);
//
// nudHeight
//
@ -62,7 +61,6 @@ private void InitializeComponent()
0,
0});
this.nudHeight.Name = "nudHeight";
this.nudHeight.ValueChanged += new System.EventHandler(this.nudHeight_ValueChanged);
//
// lblWidth
//
@ -77,8 +75,11 @@ private void InitializeComponent()
// cbAspectRatio
//
resources.ApplyResources(this.cbAspectRatio, "cbAspectRatio");
this.cbAspectRatio.Checked = true;
this.cbAspectRatio.CheckState = System.Windows.Forms.CheckState.Checked;
this.cbAspectRatio.Name = "cbAspectRatio";
this.cbAspectRatio.UseVisualStyleBackColor = true;
this.cbAspectRatio.CheckedChanged += new System.EventHandler(this.cbAspectRatio_CheckedChanged);
//
// btnOK
//

View file

@ -39,17 +39,26 @@ public partial class ImageSizeForm : Form
{
public Size Result { get; private set; }
public ImageSizeForm()
private double widthRatio, heightRatio;
private bool ignoreValueChanged = true;
public ImageSizeForm(Size size)
{
InitializeComponent();
Icon = ShareXResources.Icon;
}
public ImageSizeForm(Size size) : this()
{
Result = size;
widthRatio = (double)size.Width / size.Height;
heightRatio = (double)size.Height / size.Width;
nudWidth.SetValue(size.Width);
nudHeight.SetValue(size.Height);
VerifySize();
nudWidth.TextChanged += NudWidth_TextChanged;
nudHeight.TextChanged += NudHeight_TextChanged;
ignoreValueChanged = false;
}
private void VerifySize()
@ -57,19 +66,54 @@ private void VerifySize()
btnOK.Enabled = nudWidth.Value > 0 && nudHeight.Value > 0;
}
private void ApplyWidthAspectRatio()
{
if (!ignoreValueChanged)
{
if (cbAspectRatio.Checked)
{
ignoreValueChanged = true;
nudHeight.Value = (int)Math.Round((double)nudWidth.Value * heightRatio);
ignoreValueChanged = false;
}
VerifySize();
}
}
private void ApplyHeightAspectRatio()
{
if (!ignoreValueChanged)
{
if (cbAspectRatio.Checked)
{
ignoreValueChanged = true;
nudWidth.Value = (int)Math.Round((double)nudHeight.Value * widthRatio);
ignoreValueChanged = false;
}
VerifySize();
}
}
private void ResizeSizeForm_Shown(object sender, EventArgs e)
{
this.ForceActivate();
}
private void nudWidth_ValueChanged(object sender, EventArgs e)
private void NudWidth_TextChanged(object sender, EventArgs e)
{
VerifySize();
ApplyWidthAspectRatio();
}
private void nudHeight_ValueChanged(object sender, EventArgs e)
private void NudHeight_TextChanged(object sender, EventArgs e)
{
VerifySize();
ApplyHeightAspectRatio();
}
private void cbAspectRatio_CheckedChanged(object sender, EventArgs e)
{
ApplyWidthAspectRatio();
}
private void btnOK_Click(object sender, EventArgs e)