private void setPicture(object sender, System.EventArgs e) { double mh, mw; ChoosePictureForm dialog = (ChoosePictureForm)sender; this.pictureBox1.Image = Image.FromFile(dialog.PicturePath); if ((pictureBox1.Image.Width > pbw) || (pictureBox1.Image.Height > pbh)) { pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; mh = (double)pbh / (double)pictureBox1.Image.Height; mw = (double)pbh / (double)pictureBox1.Image.Width; if (mh < mw) { pictureBox1.Width = Convert.ToInt16(pictureBox1.Image.Width * mh); pictureBox1.Height = pbh; } else { pictureBox1.Width = pbw; pictureBox1.Height = Convert.ToInt16(pictureBox1.Image.Height * mw); } } else if (pictureBox1.SizeMode == PictureBoxSizeMode.StretchImage) { pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize; } }
private void ChoosePictureButton_Click(object sender, EventArgs e) { ChoosePictureForm dialog = new ChoosePictureForm(); dialog.Owner = this; dialog.ApplyHandler += new EventHandler(setPicture); dialog.Show(); }