将位图图像从类发送到表单

时间:2022-05-19 22:10:35

The title should explain it all really, I have a method in a class file that is currently being executed and it loads and image and stores it in a bitmap variable. My aim is to display this image to the user, so i also have a form class with a picture box. How can I load this form from a separate class, sending the form the image to store in the picture box?

标题应该真的解释一下,我在一个当前正在执行的类文件中有一个方法,它加载并映像并将其存储在一个位图变量中。我的目的是向用户显示此图像,因此我还有一个带有图片框的表单类。如何从单独的类加载此表单,将图像发送到图片框中存储?

class

public void execute()
{
    Bitmap img = new Bitmap(location);
    //add call form code here
}

form

形成

public Form1()
{
        InitializeComponent();
        //perhaps add image to picture box here
}

3 个解决方案

#1


1  

You need to set the Image property of the PictureBox with the result that you get from your class

您需要使用从类中获得的结果设置PictureBox的Image属性

myPictureBox.Image = (Image)MyClass.GetBitmap();

#2


1  

In your form class add a public property for an image.

在表单类中,为图像添加公共属性。

public Image PictureBoxImage   
{   
    set
    {
        this.pictureBox.Image = value;
    }
}

Moving on to your other class these are the steps for opening a form:

转到您的其他课程,这些是打开表单的步骤:

  1. Create the form object CustomForm form1 = new CustomeForm;
  2. 创建表单对象CustomForm form1 = new CustomeForm;
  3. In your case you need to change the picture by setting the property that you just added
    form1.PictureBoxImage = (Image)yourBitmap;
  4. 在您的情况下,您需要通过设置刚刚添加的属性来更改图片.PictureBoxImage =(Image)yourBitmap;
  5. Finally you need to display it to the user form1.Show();
  6. 最后你需要将它显示给用户form1.Show();

#3


1  

This managed to solve it,

这设法解决了它,

     public void execute()
     {
        Bitmap img = new Bitmap(location);
        Form1 f = new Form1();
        f.BackgroundImage = img;
        f.Show();
     }

For some reason it wouldn't work with the picturebox but it does with the background and thats sufficient for now.

由于某种原因,它不适用于图片框,但它与背景有关,现在已经足够了。

#1


1  

You need to set the Image property of the PictureBox with the result that you get from your class

您需要使用从类中获得的结果设置PictureBox的Image属性

myPictureBox.Image = (Image)MyClass.GetBitmap();

#2


1  

In your form class add a public property for an image.

在表单类中,为图像添加公共属性。

public Image PictureBoxImage   
{   
    set
    {
        this.pictureBox.Image = value;
    }
}

Moving on to your other class these are the steps for opening a form:

转到您的其他课程,这些是打开表单的步骤:

  1. Create the form object CustomForm form1 = new CustomeForm;
  2. 创建表单对象CustomForm form1 = new CustomeForm;
  3. In your case you need to change the picture by setting the property that you just added
    form1.PictureBoxImage = (Image)yourBitmap;
  4. 在您的情况下,您需要通过设置刚刚添加的属性来更改图片.PictureBoxImage =(Image)yourBitmap;
  5. Finally you need to display it to the user form1.Show();
  6. 最后你需要将它显示给用户form1.Show();

#3


1  

This managed to solve it,

这设法解决了它,

     public void execute()
     {
        Bitmap img = new Bitmap(location);
        Form1 f = new Form1();
        f.BackgroundImage = img;
        f.Show();
     }

For some reason it wouldn't work with the picturebox but it does with the background and thats sufficient for now.

由于某种原因,它不适用于图片框,但它与背景有关,现在已经足够了。