Monday, July 30, 2012

How to pass image from one windows form to another window form

Step-1:  Add a following reference to your project:
Sytem.Drawing


Step-2:  Make a property as following in one of your Form (say Form1) so that this property would be accessible to another form (say Form2)
      
  Image image_Form1;
        public Image Image_Form1
        {
            get { return image_Form1; }
            set { image_Form1 = value; }
        }



Step-3:  Then in your code get the image in a variable (say image_From1)
      
      image_Form1= Image.FromFile("world.bmp");


Step-4:  In your another form “Form2”, suppose you want to show the Form1’s image in a picture Box.
Make an object of Form1 in Form2.
Form1 Form1Object = new Form1();


Step-5:  Then you could assign Form1’s image to a picture Box like:
pictureBox.Image = Form1Object.Image_From1;

5 comments:

  1. Thanks alot.. It is useful for me after a long time struggle with it. GBU.

    ReplyDelete
  2. I have a picturebox which I am using a browse button to get the picture. What can i do to get it from there since i am going to use a different picture all the time ?

    Thanks

    ReplyDelete
    Replies
    1. In that case, you need to get the picture from your picture Box and assign that to 'image_Form1' variable and then transfer that image as usual using 'Image_Form1' property.

      image_Form1 = pictureBox1.Image;

      Here is the piece of code for your reference:

      private void btn_Browse_Click(object sender, EventArgs e)
      {
      DialogResult r = openFileDialog1.ShowDialog();
      if (r == System.Windows.Forms.DialogResult.OK)
      {
      pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
      }

      image_Form1 = pictureBox1.Image;
      }
      Hope it will help.

      Delete
  3. It worked on my project. Thank for your useful post :).

    ReplyDelete