Problems with three forms and picture box

-1

Hi, my scenario is as follows: - Configuration form (name, email and a photo) - Form ABC - which has a form that is opened by clicking on a Button on the ABC form - BCA Form - which has a form that is opened by clicking on a Button on the BCA form

The ABC form or the BCA form will be called based on the choice made in the Configuration form, via radio buttons.

In this configuration form you also make the choice of a jpg / png image that the user will search on the pc itself. After all the data has been filled and the photo chosen in this configuration form, the user will click on a button "Start", where based on the choice of the ratio button ABC or BCA will open their respective forms. Also, the image will be saved in a folder named logo.jpg.

In the ABC form or BCA form there is also a picture box where the saved image will be loaded into the X folder (C: \ X \ Img \ logo.jpg). That's where my problem comes in. Both form ABC or BCA has a button that takes in another form, where it will also be loaded the same picture box ... However it throws an error because it says that the image is already loaded.

Note: To 'finalize' the configuration form I use this.Visible = false; And also for the ABC and BCA forms. I think that's why it gives this error, because the image is already with a space allocated in the memory ..

Could you help me?

private void pictureBox_logo_Click(object sender, EventArgs e)
{
 OpenFileDialog file = new OpenFileDialog();
 file.Filter = "PNG|* .png|BMP|*.bmp|JPG|*.jpg;*.jpeg";
 if (file.ShowDialog() == DialogResult.OK)
{
 pictureBox_logo.ImageLocation = file.FileName
}
 pictureBox_logo.Image.Save(@"C\tools\img\logo.jpg");

}

And in the other form (which will open with the choice of radio button ABC or BCA)

public partial class frmPrincipal : Form
 {
    public frmPrincipal()
    {
        InitializeComponent();


        picturebox_principal.Load(@"C:\tools\img\logo.jpg");
     }
}
    
asked by anonymous 12.04.2016 / 20:00

1 answer

1

Using the code provided there is no assignment of the image to the PictureBox component, so when executing the line that has .Save (), a null reference error occurs.

Follow modified code:

using (OpenFileDialog openDialog = new OpenFileDialog())
{
     openDialog.Filter = "PNG|* .png|BMP|*.bmp|JPG|*.jpg;*.jpeg";
     if (openDialog.ShowDialog() == DialogResult.OK)
         pictureBox1.Image = Image.FromFile(openDialog.FileName);

     pictureBox1.Image.Save(@"C:\tools\img\ip.png");
}

Uploading the image to the other PictureBox will be done in the same way:

public Form2()
{
      InitializeComponent();
      pictureBox2.Load(@"C:\tools\img\ip.png");
}
    
19.04.2016 / 21:07