简体   繁体   English

如何将图像从Form1分配给Form2 Picturebox

[英]How to assign a image From Form1 to Form2 Picturebox

I am creating Image editor using C# Windows application, and now I have problem to assign a image to Form2 picturebox from Form1. 我正在使用C#Windows应用程序创建图像编辑器,现在我无法将图像从Form1分配给Form2图片框。

I am trying shown below code 我正在尝试显示下面的代码
The below code is written in Form1.cs page 下面的代码写在Form1.cs页面中

    if(flag==0)
    {
        Form2 f2 = new Form2(glb_image_list_arr[0]);
        f2.Show();                
    }

And the below code is written in Form2.cs page 下面的代码写在Form2.cs页面中

public Form2()
{
    InitializeComponent();
}

public Form2(Image img)
{
    this.Show();
    this.pictureBox1.Image = img;
}

In the " this.pictureBox1.Image = img; " line, I got the following error " An unhandled exception of type 'System.NullReferenceException " 在“ this.pictureBox1.Image = img; ”行中,出现以下错误“ 类型为'System.NullReferenceException的未处理的异常'

You are missing InitializeComponents() method call in Form2(Image img) constructor use 您缺少Form2(Image img)构造函数使用中的InitializeComponents()方法调用

public Form2()
{
    InitializeComponent();
}

public Form2(Image img)
{
    InitializeComponent();
    this.pictureBox1.Image = img;
}

will work. 将工作。

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

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