0

I would like to copy a picturebox to another picturebox, but I do not want them to change with each other.

PictureBox picbox1 = new PictureBox();
PictureBox picbox2 = picbox1;

picbox1.Visible = false; //The problem here is that picbox2.Visible will also become false

I would like to make picbox1 change without changing picbox2....

How would I be able to do that?

Dmitry
  • 13,797
  • 6
  • 32
  • 48
Assem Younis
  • 3
  • 1
  • 3
  • Are you after a [deep copy](http://stackoverflow.com/questions/129389/how-do-you-do-a-deep-copy-an-object-in-net-c-specifically) or are you trying to figure out how to make a picturebox have the same image as another?.. – Sayse Apr 18 '14 at 11:47
  • first you need to understand OOPS and then reference and value types and then how heap and stack based types actually works. – Sarvesh Mishra Apr 18 '14 at 11:49
  • If you can say what is the core problem you're trying to solve that will be better for us to help rather than helping to clone the picturebox. – Sriram Sakthivel Apr 18 '14 at 11:53
  • @Sayse not make them have the same image XD.... there r a few properties I would like to copy and I probably could write them all but that just felt wrong, I thought I could find a way but it seems like it would just be easier to copy them one by one... but thanks for the help! – Assem Younis Apr 18 '14 at 11:55

2 Answers2

1

Probably the best way to maintain clean code is to create an extension method, also note that your second picturebox is currently not added to controls.

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);

More can be read on extension methods here

Sayse
  • 42,633
  • 14
  • 77
  • 146
0

use this...

PictureBox p1 = new PictureBox();
PictureBox p2 = new PictureBox();
Bitmap b = new Bitmap(p1.Image);
p2.Image = b;
p1.Visible = false;
Sarvesh Mishra
  • 2,014
  • 15
  • 30