MasterSplinter
MasterSplinter

Reputation: 33

C# Changing Form1's PictureBox image to image from OpenFileDialog image selection in Form2

I'm trying to change the image currently presented in the Form1 picture box to a new image I select thru openFileDialog in Form2.

Can't seem to make it work. Help is highly appreciated, thank you.

Code: (Related functions)

**Form1.cs**

   public partial class Form1 : Form
{
    //Initializing first form:
    public Form1()
    {
        InitializeComponent();
    }

    public Form1(Image newImage)
    {
        InitializeComponent();
        picBtn.Image = newImage;
    }

    //This function is activated if the picture button was clicked:
    private void picBtn_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.ShowDialog();

    }
}

**Form2.cs**

public partial class Form2 : Form
{
    //Initializing second form:
    public Form2()
    {
        InitializeComponent();
    }

    //This function is activated if the image upload button was clicked:
    private void imageUploadBtn_Click(object sender, EventArgs e)
    {
        //New file dialog object:
        OpenFileDialog dialog = new OpenFileDialog();
        //Accept images for files only:
        dialog.Filter= "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
        dialog.ShowDialog();
        Image newImage = Image.FromFile(dialog.FileName);
        Form1 form1 = new Form1(newImage);
    }
}

Upvotes: 0

Views: 651

Answers (2)

DxTx
DxTx

Reputation: 3357

Well, you can do something like this...

Change your Form1.cs method to like this. In here, I remove your Form1(Image newImage) method and add a new public method called public void ChangePicImg(Image newImage).

public Form1()
{
    InitializeComponent();
}

public void ChangePicImg(Image newImage)
{
    picBtn.Image = newImage;
}

private void picBtn_Click(object sender, EventArgs e)
{
   Form2 form2 = new Form2();
   form2.ShowDialog();    
}

Also, change your Form2 like this.

public Form2()
{
    InitializeComponent();
}

private void imageUploadBtn_Click(object sender, EventArgs e)
{
    OpenFileDialog dialog = new OpenFileDialog();
    dialog.Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
    dialog.ShowDialog();
    Image newImage = Image.FromFile(dialog.FileName);

    Form1 frm1 = (Form1)System.Windows.Forms.Application.OpenForms["Form1"]; //Enter the Form1 name here
    frm1.ChangePicImg(newImage);
}

["Form1"] = Replace the Form1 with your Form1 name.

Upvotes: 0

Smudge202
Smudge202

Reputation: 4687

The problem here is that you have multiple instances of a Form1 . Imagine Form1 was an apple, and Form2 a orange. You have your apple on screen, which in picBtn_Click tells the computer to now also display an orange.

This orange, in imageUploadBtn_Click, tells the computer to create a new Apple containing the image you selected. However, you're not telling the existing apple to display it, and you're not asking that your second apple be shown on screen either.

Apologies for the strange analogy, but I hope it helps. What you want is for Form2 to become aware of the existing Form1. You could do this by having the Form2 constructor take a Form1 when it's created:

In Form2:

private readonly Form1 _apple;

//Initializing second form:
public Form2(Form1 apple)
{
    InitializeComponent();
    _apple = apple;
}

//This function is activated if the image upload button was clicked:
private void imageUploadBtn_Click(object sender, EventArgs e)
{
    //New file dialog object:
    OpenFileDialog dialog = new OpenFileDialog();
    //Accept images for files only:
    dialog.Filter= "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
    dialog.ShowDialog();
    Image newImage = Image.FromFile(dialog.FileName);
    // now, instead of creating a second instance of Form1,
    // pass the image to the existing instance.
    _apple.UpdatePicture(newImage);
}

You'll need to create a public method on Form1 called UpdatePicture but I leave that to you. You'll also need to have Form1 pass itself to Form2 when it creates Form2, which can be done like this:

Form2 form2 = new Form2(this);

Please note, whilst this will work, we try to avoid coupling UI elements together like this. It will work, but there are some more complicated mechanics such as MVVM that might be worth a look.

Upvotes: 1

Related Questions