To show in windows form , we will use following controls of .Net framework:
- PictureBox
- TextBox
- Button
- openFileDialog
Create new solutions and add the above controls in windows form design.
In this project we will select a image from local computer disk using openFileDialog and Button . And we will set the image path in the PictureBox.
Here we will use
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
to set full image in fixed sized pictureBox.
Â
private void upload_button_Click(object sender, EventArgs e)
{
try
{
// open file dialog
OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp;*.png)|*.jpg; *.jpeg; *.gif; *.bmp;*.png";
if (open.ShowDialog() == DialogResult.OK)
{
// display image in picture box
pictureBox1.Size = new Size(150, 150);
pictureBox1.Image = new Bitmap(open.FileName);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
// image file path
textBox1.Text = open.FileName;
}
}
catch (Exception)
{
throw new ApplicationException("Eerror in image loading....");
}
}
Â
Comments