PictureBox imageBox = new PictureBox(); imageBox.Image = Image.FromFile("image.jpg"); imageBox.SizeMode = PictureBoxSizeMode.StretchImage; this.Controls.Add(imageBox);
private void button1_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "Image Files (*.bmp;*.jpg;*.jpeg)|*.bmp;*.jpg;*.jpeg"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { PictureBox imageBox = new PictureBox(); imageBox.Image = Image.FromFile(openFileDialog1.FileName); imageBox.SizeMode = PictureBoxSizeMode.StretchImage; this.Controls.Add(imageBox); } }This code creates a button that opens a file dialog to select an image file, creates a new PictureBox control, loads the selected image file into it, sets the picture to stretch to fit the size of the box, and adds it to the form. The package library for the System.Windows.Forms PictureBox control is part of the .NET Framework, specifically the System.Windows.Forms namespace. This library contains many other controls and classes useful for creating Windows Forms applications.