Tuesday, February 9, 2010

How to load Image in C# and set properties of the Picture Box

Create a C# application drag a picture Box, four buttons and open file dialog on the form.
Write code on btn_browse Button click
-----------------------------------------
private void btn_browse_Click(object sender, System.EventArgs e)
{
try
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog()==DialogResult.OK)
{
pictureBox1.Image = new Bitmap(open.FileName);
}
}
catch (Exception)
{
throw new ApplicationException("Failed loading image");
}
}
Write code on btn_StretchImage Button click
------------------------------------------------
private void btn_StretchImage_Click(object sender, System.EventArgs e)
{
pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
}
Write code on btn_AutoSize Button click
-------------------------------------------------
private void btn_AutoSize_Click(object sender, System.EventArgs e)
{
pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
}
Write code on btn_CenterImage Button click
--------------------------------------------------
private void btn_CenterImage_Click(object sender, System.EventArgs e)
{
pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
}

No comments: