0

I am using this code to zoom & pan my Image control.

I want to know how can I revert back the control to it's original state.

After The user decided to change the picture, the Image box has to be restored to it's original location and state, so he/she can start zooming properly and fresh.

I tried using something like this but still no luck:

Image OriginalPic;
...
...
InitializeComponents();
OriginalPic = MainPic;
...
...
void ChangePic(){
MainPic = OriginalPic; // Doesn't work :(
...
}
Community
  • 1
  • 1
xperator
  • 2,743
  • 7
  • 34
  • 58

3 Answers3

0

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!

Community
  • 1
  • 1
bash.d
  • 13,029
  • 3
  • 29
  • 42
  • Can't get your first part working. I never used Serialization before :( It throws an exception at `formatter.Serialize` line – xperator May 11 '13 at 20:40
0

Change OriginalPic = MainPic; to: OriginalPic = MainPic.Clone();

Bolu
  • 8,696
  • 4
  • 38
  • 70
0

Ok I have tried many ways to deep copy the image control. But it seems there is no function that could work out of the box. However bash.d has an idea.

  • Tried using XamplWriter and XamlReader.
  • Deep copying it's parent object won't work either.
  • Tried using ObjectExtensions.cs which was supposed to work on any object.

Here is what I did to revert the transformation:

void ResetTransformationOfImage()
        {
            TransformGroup group = new TransformGroup();

            ScaleTransform xform = new ScaleTransform();
            group.Children.Add(xform);

            TranslateTransform tt = new TranslateTransform();
            group.Children.Add(tt);

            MainPic.RenderTransform = group;
        }

Anyway I will look forward to see if anyone can implement such copy function and mark it as the real answer, even though my problem is solved as for now.

Thanks.

Community
  • 1
  • 1
xperator
  • 2,743
  • 7
  • 34
  • 58