我想将一个图片框复制到另一个图片框,但我不希望它们彼此改变。
PictureBox picbox1 = new PictureBox();
PictureBox picbox2 = picbox1;
picbox1.Visible = false; //The problem here is that picbox2.Visible will also become false
我想在不更改picbox1
....
picbox2
更改
我怎么能这样做?
答案 0 :(得分:1)
维护干净代码的最佳方法可能是创建扩展方法,同时请注意您的第二个图片框当前未添加到控件中。
public static class MyExtensions
{
public static PictureBox CreateNewWithAttributes(this PictureBox pb)
{
return new PictureBox { Image = pb.Image, Width = pb.Width };
}
}
Picturebox p2 = p1.CreateNewWithAttributes();
this.Controls.Add(p2);
有关扩展方法here
的更多内容答案 1 :(得分:0)
使用此...
PictureBox p1 = new PictureBox();
PictureBox p2 = new PictureBox();
Bitmap b = new Bitmap(p1.Image);
p2.Image = b;
p1.Visible = false;