From your little code provided, I assume that both MainPic
and OriginalPic
will reference the same object -> changes to one reference will affect the other, too. You would actually need to create a backup-picture that holds the original Information, you would need to create a deep copy
of your pic.
Referring to this post:
public static T DeepClone<T>(T obj)
{
using (var ms = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
ms.Position = 0;
return (T) formatter.Deserialize(ms);
}
}
This will create a deep copy of your Image
which you can use to revert the image to its original state.
Also, I found a tutorial on using the ICloneable
-Interface:
To get a Deep Copy of you object, you have to implement IClonable interface for Invoice and all of it 's related classes:
public class Invoice: IClonable
{
public int No;
public DateTime Date;
public Person Customer;
//.............
public object Clone()
{
Invoice myInvoice = (Invoice)this.MemberwiseClone();
myInvoice.Customer = (Person) this.Customer.Clone();
return myInvoice;
}
}
public class Person: IClonable
{
public string Name;
public int Age;
public object Clone()
{
return this.MemberwiseClone();
}
}
EDIT
It seems that System.Windows.Controls.Image
cannot be serialized... You could try to derive from it and implement ISerializable
or create a (static
)method and create a clone manually. Yet, any of those steps is necessary!