提示コードですが以下のif文の中でX,yどちらかの値が変わったらそれに合わせてアスペクト比を維持たままもう片方の値を変えたいのですが以下のように実装したいのですがうまく実装できません、これはどうやるのでしょうか?アスペクト比をを算出するプログラムは作成したのですがこれを用いてどうやって実装するのでしょうか?
cs
1 2 3 private void textBoxWidth_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) 4 { 5 6 FileData data = mainForm.getSelectFileData(); 7 float aspect = data.getHeight() / data.getWidth(); //アスペクト比 8 9 if (checkBoxLookAspect.CheckState == CheckState.Checked) 10 { 11 int width = data.getWidth() - int.Parse(textBoxWidth.Text); 12 data.setHeight(data.getHeight() - width); 13 14 textBoxHeight.Text = data.getHeight().ToString(); 15 } 16 else 17 { 18 int width = int.Parse(textBoxWidth.Text); 19 data.setWidth(width); 20 textBoxWidth.Text = data.getWidth().ToString(); 21 } 22 } 23 24 private void textBoxHeight_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) 25 { 26 FileData data = mainForm.getSelectFileData(); 27 float aspect = data.getHeight() / data.getWidth(); //アスペクト比 28 29 if (checkBoxLookAspect.CheckState == CheckState.Checked) 30 { 31 int height = data.getHeight() - int.Parse(textBoxHeight.Text); 32 data.setWidth(data.getWidth() - height); 33 34 textBoxWidth.Text = data.getWidth().ToString(); 35 } 36 else 37 { 38 int height = int.Parse(textBoxHeight.Text); 39 data.setHeight(height); 40 textBoxHeight.Text = data.getHeight().ToString(); 41 } 42 } 43 44
アスペクト比を算出するプログラム
cs
1public class Program 2{ 3 public static int gcd(int x,int y) 4 { 5 if (y == 0) 6 { 7 return x; 8 } 9 10 return gcd(y, x % y); 11 } 12 13 public static void Main() 14 { 15 ImageMagick.MagickImage img = new ImageMagick.MagickImage("input.HEIC"); 16 17 /* 18 Console.WriteLine(img.Width); 19 Console.WriteLine(img.Height); 20 Console.WriteLine(img.Density.X); 21 Console.WriteLine(img.Density.Y); 22 */ 23 24 int r = gcd(img.Width, img.Height); 25 Console.WriteLine(r); 26 27 28 Console.WriteLine(img.Height / r); 29 Console.WriteLine(img.Width / r); 30 31 // Console.WriteLine(img.Height / img.Width); 32 // Console.WriteLine(img.Width / img.Height); 33 34 35 36 37 Console.ReadKey(); 38 } 39 40} 41
数学というか算数レベルの問題です。
変更前を X1,Y1 変更後を X2,Y2 とすると X1÷Y1 = X2÷Y2 が成り立ちます。
ここから X2 = ?, Y2=? が求まります。

すいませんもう少し詳細にいいですか?
「方程式の解き方をマスターしよう」
https://alpha-katekyo.jp/tips/tips025/
