C# 如何将图像从窗体传递到另一窗体

C# 如何将图像从窗体传递到另一窗体,c#,winforms,C#,Winforms,所以我有两张表格和 这是form1的外观: 如果单击“浏览图像”,它将显示以下内容: 这是我第一张表格中的代码: private void browseimage_Click(object sender, EventArgs e) { OpenFileDialog result = new OpenFileDialog(); if (result.ShowDialog() == System.Windows.Forms.DialogResult.OK) { }

所以我有两张表格和

这是form1的外观:

如果单击“浏览图像”,它将显示以下内容:

这是我第一张表格中的代码:

private void browseimage_Click(object sender, EventArgs e)
{
    OpenFileDialog result = new OpenFileDialog();

    if (result.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
    }
}

如果要存储我拾取的图像文件,然后将值传递给第二个表单,请在中。

将图像存储在图像类型变量中,然后由第二个表单的构造函数传递该变量。 大概是这样的:

Image myImage = Image.FromFile("C:\\... 
Pathtotheimage... ");
MyForm form = new MyForm(myImage);
Public MyForm (Image image) {
//do something here with the image 
} 
在第二个窗体的构造函数端。这样做:

Image myImage = Image.FromFile("C:\\... 
Pathtotheimage... ");
MyForm form = new MyForm(myImage);
Public MyForm (Image image) {
//do something here with the image 
} 
我没有尝试编译这段代码,但是您可以将图像传递到第二个表单


希望它能有所帮助

另一种方法是为表单中的图像创建公共属性2:

public partial class Form2 : Form
{
    public Image ImageFromForm1 { get; set; }

    // rest of code omitted...
然后,当您从用户处获取路径并创建表单实例时,可以如下设置属性:

private void browseimage_Click(object sender, EventArgs e)
{
    var imageFileTypes = new List<string>
    {
        "Bitmap|*.bmp;*.dib",
        "JPEG|*.jpg;*.jpeg;*.jpe;*.jfif",
        "GIF|*.gif",
        "TIFF|*.tif;*.tiff",
        "PNG|*.png",
        "ICO|*.ico",
        "All Images|*.bmp;*.dib;*.jpg;*.jpeg;*.jpe;*.jfif;*.gif;*.tif;*.tiff;*.png;*.ico"
    };

    OpenFileDialog result = new OpenFileDialog
    {
        InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures),
        Filter = string.Join("|", imageFileTypes),
        FilterIndex = imageFileTypes.Count
    };

    if (result.ShowDialog() == DialogResult.OK)
    {
        var form2 = new Form2();
        form2.ImageFromForm1 = Image.FromFile(result.FileName);
        form2.Show();
    }
}

很难告诉你如何修复代码中的问题,因为你没有发布代码,也没有像Sergey所写的那样提出任何问题细节,如果你不放置代码,这将很难。实现将数据从一个表单发送到另一个表单的一种方法是更改表单的构造函数以接收图像,然后在创建表单实例时将其发送。您还可以在Form2上创建一个公共属性,该属性表示图像的文件路径,在实例化Form2对象后,可以从Form1中设置。非常感谢您的帮助: