Load image to ImageBox : PictureBox « GUI Windows Form « C# / C Sharp






Load image to ImageBox

Load image to ImageBox
  using System;
  using System.Drawing;
  using System.Windows.Forms;

  public class MyForm : System.Windows.Forms.Form
  {
    private Button btnLoad;  
    private PictureBox imgPhoto;

    public MyForm()
    {
      this.Text = "www.java2s.com";

      btnLoad = new Button();
      btnLoad.Text = "&Load";
      btnLoad.Left = 10;
      btnLoad.Top = 10;
      btnLoad.Click += new System.EventHandler(this.OnLoadClick);

      imgPhoto = new PictureBox();
      imgPhoto.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      imgPhoto.Width = this.Width / 2;      
      imgPhoto.Height = this.Height / 2;      
      imgPhoto.Left = (this.Width - imgPhoto.Width) / 2;
      imgPhoto.Top = (this.Height - imgPhoto.Height) / 2;
      imgPhoto.SizeMode = PictureBoxSizeMode.StretchImage;

      this.Controls.Add(btnLoad);
      this.Controls.Add(imgPhoto);  
    }

    protected void OnLoadClick(object sender, System.EventArgs e)
    {
      OpenFileDialog dlg = new OpenFileDialog();

      dlg.Title = "Open Image";
      dlg.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*" ;

      if (dlg.ShowDialog() == DialogResult.OK)
      {
        imgPhoto.Image = new Bitmap(dlg.OpenFile());
      }

      dlg.Dispose();
    }


    public static void Main()
    {
      Application.Run(new MyForm());
    }
  }



           
       








Related examples in the same category

1.PictureBox Click eventPictureBox Click event
2.PictureBox Scroll Text
3.PictureBox visible and invisiblePictureBox visible and invisible
4.PictureBox DemoPictureBox Demo
5.SizeMode in PictureBox
6.Subclass PictureBox
7.Dragging ImagesDragging Images