首先,创建2个Form窗体,各放一个pictureBox.
将pictureBox的属性调整为:StretchImage。
form1代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DelegateShow_0319
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Picture_Show();
}
Bitmap bit;//保存图像的Bitmap对象
private void Picture_Show()
{
bit = new Bitmap("123.bmp");//运行前,请拷贝一张图片到当前执行目录。
this.pictureBox1.Image = bit;//显示图片。
}
private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
Form2 form2 = new Form2(bit);//构造传参
form2.Form2_ChangePicture += new Form2.ChangePictureHandler(Form1_Change_Picture);//使用委托,关闭子窗体时,图片回传
form2.Show();
this.pictureBox1.Image = null;
}
private void Form1_Change_Picture(Bitmap bit)
{
this.pictureBox1.Image = bit;
}
}
}
需要添加pictureBox的DoubleClick事件。
form2 代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DelegateShow_0319
{
public partial class Form2 : Form
{
public delegate void ChangePictureHandler(Bitmap bit);
public event ChangePictureHandler Form2_ChangePicture;
private Bitmap BitImage;
public Form2(Bitmap bit)
{
InitializeComponent();
BitImage = bit;
this.pictureBox2.Image = bit;
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
if (Form2_ChangePicture != null)
{
Form2_ChangePicture(BitImage);
}
}
}
}
需要添加Form2 formClosing事件。
设置好后运行:
然后双击图片:
点击关闭按钮,子窗体退出,原窗体显示图片。