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
  • 367
  • 5
  • 17

2 Answers2

3

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

Bitmap copy = new Bitmap(image);
C.Evenhuis
  • 25,310
  • 2
  • 59
  • 70
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
  • 367
  • 5
  • 17