将图像从 Form2 传递到 Form1 [英] Passing Image from Form2 to Form1

查看:36
本文介绍了将图像从 Form2 传递到 Form1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将第二个表单中 PictureBox 中的图像传递到我已经打开的第一个表单中.

我正在使用此代码:

private void btnCancel_Click(object sender, EventArgs e){var frmAdd = new frmAdd();frmAdd.pctImage.Image = pctImage.Image;if (btnCancel.Text == "C&OPY"){this.Close();返回;}if (btnCancel.Text == "C&ANCEL"){this.Close();返回;}}

我希望有人能帮我解决这个问题.谢谢.

解决方案

根据 文档,当您将图像从一种形式传递到另一种形式时,您应该创建图像的克隆.类似的东西:

frmAdd.pctImage.Image = pctImage.Image.Clone() as Image;

正如 lumberjack4 指出的那样,您还创建了一个新的、不可见的 frmAdd 并将您的图像分配给该表单,而不是已显示的那个.图像实际上可能被正确分配(尽管您仍然应该克隆它)但它永远不会在屏幕上可见,因为您的本地 frmAdd 永远不会显示.这里有一些代码会告诉你如何去做:

frmAdd中---------:

公共部分类 frmAdd : 表单{//存储对当前显示的 frmAdd 实例的引用.私有静态 frmAdd s_oInstance = null;//返回对当前显示的 frmAdd 实例的引用//如果 frmAdd 未显示,则为 null.静态,所以其他形式可以//访问它,即使实例不可用.公共静态 frmAdd 实例{得到{返回 ( s_oInstance );}}公共 frmAdd(){初始化组件();}//设置指定图片.这是必要的,因为 picAdd//是私有的,公开它不是一个好主意.public void SetPicture ( Image oImage ){picAdd.Image = oImage;}//这些事件处理程序用于跟踪 frmAdd 实例//可用.如果是,则更新私有静态引用//以便该实例可用于其他表单.private void frmAdd_Load(对象发送者,EventArgs e){//表单已加载(不一定可见).s_oInstance = 这个;}private void frmAdd_FormClosed(对象发送者,FormClosedEventArgs e){//表单已关闭.s_oInstance = null;}//任何你需要的其他代码}

在 frmNew ---------:

公共部分类 frmNew : 表单{公共 frmNew(){初始化组件();}private void btnCancel_Click(对象发送者,EventArgs e){//是否有 frmAdd 的活动实例?如果是,//使用所用图像的副本调用 SetPicture()//这个 frmNew.frmAdd oFrm = frmAdd.Instance;如果 ( oFrm != null )oFrm.SetPicture ( picNew.Image.Clone () as Image );}}

涉及到 2 个 PictureBox 控件:frmAdd 上的 picAddfrmNew 上的 picNew代码>.当 btnCancel 被点击时,frmNew 中的代码检查是否有一个有效的 frmAdd 实例,如果是,它设置它的图像.

请注意,picAdd 不是公共控件 - 它应该是私有的.在表单中将控件设置为 public 不是一个好主意,因为它允许对它们进行不受控制的访问,并且表单肯定不会知道它们的状态(因为其他人可能会在表单不知道的情况下更改这些控件.)这可能导致变化困难- 修复大型程序中的错误.

如果您需要访问窗体之外的控件,请将控件保留为私有并在窗体中创建公共属性/方法,以便在必要时更新控件 - 就像上面的 SetPicture 方法.这仍然允许您从表单外部分配图片,但表单可以控制如何发生,因为 SetPicture 可以验证图像等.如果您只是将控件设置为公开,这是不可能的.>

I can't pass the image in my PictureBox in my 2nd form into my first form which is already opened.

I'm using this code:

private void btnCancel_Click(object sender, EventArgs e)
{
    var frmAdd = new frmAdd();

    frmAdd.pctImage.Image = pctImage.Image;

    if (btnCancel.Text == "C&OPY")
    {
        this.Close();
        return;
    }
    if (btnCancel.Text == "C&ANCEL")
    {
        this.Close();
        return;
    }
}

I hope someone could help me with this. Thanks.

解决方案

According to the documentation, you should create a clone of the image when you're passing it to from one form to the other. Something like:

frmAdd.pctImage.Image = pctImage.Image.Clone() as Image;

Edit: As lumberjack4 pointed out, you also create a new, invisible frmAdd and assign your image to that form, not to the one already shown. The image may actually be correctly assigned (although you should still clone it) but it's never visible on the screen because your local frmAdd is never shown. Here's some code that would tell you how to do it:

In frmAdd ---------:

public partial class frmAdd : Form
{
    // Stores a reference to the currently shown frmAdd instance.
    private static frmAdd s_oInstance = null;

    // Returns the reference to the currently shown frmAdd instance
    // or null if frmAdd is not shown. Static, so other forms can
    // access this, even when an instance is not available.
    public static frmAdd Instance
    {
        get
        {
            return ( s_oInstance );
        }
    }

    public frmAdd ()
    {
        InitializeComponent ();
    }

    // Sets the specified picture. This is necessary because picAdd
    // is private and it's not a good idea to make it public.
    public void SetPicture ( Image oImage )
    {
        picAdd.Image = oImage;
    }

    // These event handlers are used to track if an frmAdd instance
    // is available. If yes, they update the private static reference
    // so that the instance is available to other forms.
    private void frmAdd_Load ( object sender, EventArgs e )
    {
        // Form is loaded (not necessarily visible).
        s_oInstance = this;
    }

    private void frmAdd_FormClosed ( object sender, FormClosedEventArgs e )
    {
        // Form has been closed.
        s_oInstance = null;
    }

    // Whatever other code you need
}

In frmNew ---------:

public partial class frmNew : Form
{
    public frmNew ()
    {
        InitializeComponent ();
    }

    private void btnCancel_Click ( object sender, EventArgs e )
    {
        // Is there an active instance of frmAdd? If yes,
        // call SetPicture() with a copy of the image used by
        // this frmNew.
        frmAdd oFrm = frmAdd.Instance;

        if ( oFrm != null )
            oFrm.SetPicture ( picNew.Image.Clone () as Image );
    }
}

There are 2 PictureBox controls involved: picAdd on frmAdd and picNew on frmNew. When btnCancel is clicked, the code in frmNew checks if there's a valid frmAdd instance and if yes, it sets its image.

Note that picAdd is not a public control - it's private, as it should be. It's not a good idea to set controls to public in forms because it allows uncontrolled access to them and the form won't know their state for sure (since someone else may change those controls without the form knowing.) This can lead to vary hard-to-fix errors in larger programs.

If you need to access a control outside its form, leave the control private and create a public property/method in the form that updates the control when necessary - like the above SetPicture method. This still lets you assign the picture from outside the form but the form has control over how that happens because SetPicture can validate the image, etc. This is not possible if you just set your controls public.

这篇关于将图像从 Form2 传递到 Form1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆