public static string getImageAspectRatioAsFraction(int x, int y) { double ratio = (double)x / (double)y; mMath math = new mMath(); Fraction fraction = math.RealToFraction(ratio, 0.01); return(fraction.N.ToString() + ":" + fraction.D.ToString()); }
private void validateTextboxInput(TextBox textbox) { //This function can be called to check user input values for X and Y resolution and correct them if necessary if (textbox == textBoxResizeNewX) { try { mMath mMath = new mMath(); int newX = mMath.LimitToRange(int.Parse(textBoxResizeNewX.Text), Program.MinX, Program.MaxX); //Limit user input to permitted range if (checkBoxResizeLockAspect.Checked) { //When aspect ratio is locked, automatically update Y-value to match X-input double ratio = Program.getImageAspectRatio(); int newY = (int)Math.Round(newX / ratio); if (newY < Program.MinY || newY > Program.MaxY) { //If newX is too large, it has to be constrained and newY recalculated newY = new mMath().LimitToRange(newY, Program.MinY, Program.MaxY); newX = (int)Math.Round(newY * ratio); } textBoxResizeNewX.Text = newX.ToString(); textBoxResizeNewY.Text = Math.Round(newX / ratio).ToString(); labelResizeNewAspect.Text = Program.getImageAspectRatioAsFraction(newX, newY); return; } else { int newY = int.Parse(textBoxResizeNewY.Text); textBoxResizeNewX.Text = newX.ToString(); labelResizeNewAspect.Text = Program.getImageAspectRatioAsFraction(newX, newY); return; } } catch (Exception ex) { Debug.WriteLine(TAG + ": " + ex.Message); } } else if (textbox == textBoxResizeNewY) { try { mMath mMath = new mMath(); int newY = mMath.LimitToRange(int.Parse(textBoxResizeNewY.Text), Program.MinY, Program.MaxY); if (checkBoxResizeLockAspect.Checked) { //When aspect ratio is locked, automatically update X-value to match Y-input double ratio = Program.getImageAspectRatio(); int newX = (int)Math.Round(newY * ratio); if (newX < Program.MinX || newX > Program.MaxX) { //If newX is too large, it has to be constrained and newY recalculated newX = new mMath().LimitToRange(newX, Program.MinX, Program.MaxX); //check and clamp newX value if needed newY = (int)Math.Round(newX / ratio); } textBoxResizeNewY.Text = newY.ToString(); textBoxResizeNewX.Text = newX.ToString(); labelResizeNewAspect.Text = Program.getImageAspectRatioAsFraction(newX, newY); return; } else { int newX = int.Parse(textBoxResizeNewX.Text); double ratio = newX / newY; textBoxResizeNewY.Text = newY.ToString(); labelResizeNewAspect.Text = Program.getImageAspectRatioAsFraction(newX, newY); return; } } catch (Exception ex) { Debug.WriteLine(TAG + ": " + ex.Message); } } }