无法在Windows窗体中加载图像C#

时间:2021-01-01 18:06:42

I have 2 windowsForms lets call them form1 and form2. form1 consists of a picturebox and a button. When I click on the button form2 opens. Now form2 consists of a grid and depending on the position on which I have clicked on the grid the x and y coordinates are returned to form1. Based on these coordinates I add an image to the pictureBox. But the image isn't getting added! Is there something I am missing here?

我有2个windowsForms让我们称之为form1和form2。 form1由一个图片框和一个按钮组成。当我点击按钮form2打开。现在form2由一个网格组成,根据我点击网格的位置,x和y坐标返回到form1。基于这些坐标,我将图片添加到pictureBox。但图像没有添加!这里有什么我想念的吗?

Code inside the mouseDownEvent in form2

form2中mouseDownEvent内的代码

Form1 f1 = new Form1();
f1.openImage(x,y);

Code in form1

form1中的代码

internal void openImage(int x, int y)
    {
        string ogFileName = "r" + x.ToString() + "c" + y.ToString();
        string imageFilePath = ogFileName  + "." + extension;
        MessageBox.Show(imageFilePath); //I can see the correct path here
        pictureBox1.Image = Image.FromFile(imageFilePath);
}//extension is a static variable declared outside this function.

1 个解决方案

#1


1  

Form1 f1 = new Form1();

This line only creates a new instance of Form1

此行仅创建Form1的新实例

What you can do is adding a Form1 variable which will reference your current form.

您可以做的是添加一个Form1变量,该变量将引用您当前的表单。

You initialize it in constructor and in the button click you pass it to Form2

您在构造函数中初始化它,然后在按钮单击中将它传递给Form2

public partial class Form1 : Form
{
   Form1 form1; // form1 will store the reference of Form1
   public Form1()
   {
    form1 = this; // We initialize form1 in the constructor
    InitializeComponent();
   }

   // button to open form2
   private void button1_Click(object sender, EventArgs e)
   {
       Form2 form2 = new Form2(form1); // We open form2 with form1 as parameter
        form2.Visible = true;
    }


    public void openImage(int x, int y)
    {

    }

}

Now in the Form2 you just have to add a Form1 variable that will be initialized in the constructor.

现在在Form2中,您只需要添加一个将在构造函数中初始化的Form1变量。

Then you can use it as now it represents the current instance of Form1

然后你可以使用它,因为它现在代表Form1的当前实例

public partial class Form2 : Form
{
    Form1 form1; // Reference to form1

    public Form2(Form1 form1)
    {
        this.form1 = form1; // We initialize form1
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // we call openimage from form1
        form1.openImage(130, 140);
    }
}

I have tested this example with a label and it works fine so I think there should be no problem with a picturebox

我用标签测试了这个例子,它工作正常,所以我觉得应该没有问题

#1


1  

Form1 f1 = new Form1();

This line only creates a new instance of Form1

此行仅创建Form1的新实例

What you can do is adding a Form1 variable which will reference your current form.

您可以做的是添加一个Form1变量,该变量将引用您当前的表单。

You initialize it in constructor and in the button click you pass it to Form2

您在构造函数中初始化它,然后在按钮单击中将它传递给Form2

public partial class Form1 : Form
{
   Form1 form1; // form1 will store the reference of Form1
   public Form1()
   {
    form1 = this; // We initialize form1 in the constructor
    InitializeComponent();
   }

   // button to open form2
   private void button1_Click(object sender, EventArgs e)
   {
       Form2 form2 = new Form2(form1); // We open form2 with form1 as parameter
        form2.Visible = true;
    }


    public void openImage(int x, int y)
    {

    }

}

Now in the Form2 you just have to add a Form1 variable that will be initialized in the constructor.

现在在Form2中,您只需要添加一个将在构造函数中初始化的Form1变量。

Then you can use it as now it represents the current instance of Form1

然后你可以使用它,因为它现在代表Form1的当前实例

public partial class Form2 : Form
{
    Form1 form1; // Reference to form1

    public Form2(Form1 form1)
    {
        this.form1 = form1; // We initialize form1
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // we call openimage from form1
        form1.openImage(130, 140);
    }
}

I have tested this example with a label and it works fine so I think there should be no problem with a picturebox

我用标签测试了这个例子,它工作正常,所以我觉得应该没有问题