0

I need to make copy of Image class. Tried clone(), but didn't work. Also googled about it and found MemberwiseClone(), but i think i can't access Image class. Also tried this solution, guess it didn't work. Any ideas how to copy Image class without reference?

Community
  • 1
  • 1
Mažas
  • 387
  • 1
  • 6
  • 19

2 Answers2

3

Bitmap (which inherits from Image has a constructor overload that creates a copy:

Bitmap copy = new Bitmap(image);
C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72
0

My bad. There was silly mistake. Solution below also works fine.

private Image DeepCopy(Image other)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(ms, other);
                ms.Position = 0;
                return (Image)formatter.Deserialize(ms);
            }
        }

.

Image tempImg = DeepCopy(img);

Also, this works too:

Image tempImg = (Image)img.Clone();
Mažas
  • 387
  • 1
  • 6
  • 19