简体   繁体   English

C#将Form1的PictureBox图像更改为Form2中OpenFileDialog图像选择中的图像

[英]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. 我试图将当前在Form1图片框中显示的图像更改为通过Form2中的openFileDialog选择的新图像。

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);
    }
}

The problem here is that you have multiple instances of a Form1 . 这里的问题是您有Form1多个实例 Imagine Form1 was an apple, and Form2 a orange. 想象一下Form1是一个苹果, Form2是一个橙色。 You have your apple on screen, which in picBtn_Click tells the computer to now also display an orange. 您的屏幕上有苹果,在picBtn_Click告诉计算机现在也显示橙色。

This orange, in imageUploadBtn_Click , tells the computer to create a new Apple containing the image you selected. imageUploadBtn_Click橙色,告诉计算机创建一个包含所选图像的new Apple 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 . 您想要的是让Form2知道现有的Form1 You could do this by having the Form2 constructor take a Form1 when it's created: 您可以通过在创建Form2构造函数时使用Form1来做到这一点:

In Form2 : 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. 您需要在Form1上创建一个名为UpdatePicturepublic方法,但我将其留给您。 You'll also need to have Form1 pass itself to Form2 when it creates Form2 , which can be done like this: 您还需要有Form1本身传递给Form2时,它创建Form2 ,它可以这样做:

Form2 form2 = new Form2(this);

Please note, whilst this will work, we try to avoid coupling UI elements together like this. 请注意,尽管这可行,但我们尽量避免 UI元素像这样耦合在一起。 It will work, but there are some more complicated mechanics such as MVVM that might be worth a look. 它将起作用,但是有些更复杂的机制(例如MVVM)可能值得一看。

Well, you can do something like this... 好吧,你可以做这样的事情...

Change your Form1.cs method to like this. 将您的Form1.cs方法更改为这样。 In here, I remove your Form1(Image newImage) method and add a new public method called public void ChangePicImg(Image newImage) . 在这里,我删除了您的Form1(Image newImage)方法,并添加了一个名为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. 另外,像这样更改您的Form2

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. ["Form1"] =用您的Form1名称替换Form1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM