Click here to Skip to main content
15,916,846 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello i have two form in my application and want to pass the image from one form to another form. suppose my first form is registration in which have a picture box. and i am capturing the image i another form(captureform). now i want to send the captured image from captureform to registration form. without reloading the registration form.

What I have tried:

here i have tried two thing first by assign the picture box with another picturebox like
Registration frmreg = new Registration();
frmreg.imgUpload.Image = imgPreview.Image;

but this is not work for me after that i have tried with method with image parameter here data is coming but cant display in picture box like
in Captureform
Registration frmreg = new Registration();
frmreg.DisplayImage(imgPreview.Image);

and in image form i have created a method for it.
C#
public void DisplayImage(Image upload)
     {
         imgUpload.Image = upload;
     }

here data is coming in upload but not display in my picturebox
Posted
Updated 4-Nov-16 7:57am

Don't create a new instance each time: reuse the current.
This should help: Transferring information between two forms, Part 1: Parent to Child[^]
But...
Add a class level Registration variable:
C#
private Registration frmReg = null;
Then, when you need to open the form, do this:
C#
if (frmReg == null)
    {
    frmReg = new Registration();
    frmReg.FormClosing += frmReg_FormClosing;
    }

And add a simple handler:
C#
void frmReg_FormClosing(object sender, FormClosingEventArgs e)
    {
    frmReg = null;
    }

That way, you have a record of the current instance, and if it's closed, you throw it away for next time.
You can then add a property to the Registration form which takes an Image and app it to it:
C#
if (frmReg != null)
   {
   frmReg.UserPicture = myImage;
   }
 
Share this answer
 
Why don't You use OpenFileDialog class[^] for importing images from files to the Registration form picture box control ?

Any way, this example works fine :

C#
using System;
using System.Windows.Forms;

namespace Pass_image_from_Capture_to_Registration_form
{
	/// <summary>
	/// Description of Registration Form.
	/// 
	/// Registration Form is main ( parrent ) form of application
	/// that contains it's own Registration_Form_Image picture box control
	/// </summary>
	public partial class Registration_Form : Form
	{
		
		// Create new Capture form that contains Image_Preview picture box
		// with initial image as child form of application
		//
		private Capture_Form Capture = new Capture_Form();
		
		public Registration_Form()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// Set image to Registration Form from Capture form
			//
			Registration_Form_Image.Image = Capture.Image_Preview.Image;
		}
	}
}


Remember to set modifiers property of Capture Form picture box control to public so it can be modified or accessed from the Registration form.

All the best,
Željko Perić
 
Share this answer
 
v2
Something I discovered about images when I was writing my SafeImage article - If you load an image into a form or control on the form, when the form closes, that image gets disposed. If you are trying to pass that image off somewhere else, you're going to get into trouble. You do need to make a copy of it before you pass it off.
C#
Bitmap copy = Bitmap.FromImage(imgPreview.Image);
frmReg.UploadImage.Image = copy;

If you don't do that, then when captureForm is closed, the image will be disposed, and all sorts of weird stuff is likely to start happening.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900