Having problems with Image Manipulation when loading from a xml file (Windows 8 c#)

  • Thread starter Thread starter TAPereira
  • Start date Start date
T

TAPereira

Guest
Im develloping a program, with Windows 8 c#, that saves and loads .xml file where i save images paths and their coordinates on a canvas.

The situation here is that i want to have an edit button to update the image coordinates, through manipulating it, and when i receive a xml file with just one image, i have no problem. It draws the image in perfection. But when i have to load a xml that have 2 or more images, i have problems during their manipulation: i only can manipulate the last image that i load on canvas. (by the way i dont have any problem with load or save xml files)

Can someone help me?

Assume that xml files are load correctly and i have everything that i need to load the files, because as i said if my xml just have 1 image, i dont have any problem with it. Here is some of my code:

public async void LoadImagesToEdit(Canvas canvas)
{
folder = await KnownFolders.PicturesLibrary.GetFolderAsync("CaiMUfiles\\output\\Recompensas\\");

for (int i = 0; i < c.ImageContent.Count; i++) //ImageContent is a list of a class(ImageMath) where i load/save the image coordinates, his path and other stuff
{
StorageFile file = await folder.GetFileAsync(c.ImageContent.filename);
Image image = new Image();

if (file != null)
{
using (IRandomAccessStream fileStream = file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
BitmapImage img = new BitmapImage();
img.SetSource(fileStream);
image.Source = img;
}

image.ManipulationMode = Windows.UI.Xaml.Input.ManipulationModes.All;
image.ManipulationStarting += new ManipulationStartingEventHandler(Image_ManipulationStarting);
image.ManipulationStarted += new ManipulationStartedEventHandler(Image_ManipulationStarted);
image.ManipulationDelta += new ManipulationDeltaEventHandler(Image_ManipulationDelta);
image.ManipulationCompleted += new ManipulationCompletedEventHandler(Image_ManipulationCompleted);
image.ManipulationInertiaStarting += new ManipulationInertiaStartingEventHandler(Image_ManipulationInertiaStarting);
InitImageManipulationTransforms(image);

ImageMath im = new ImageMath()
{
dragTranslation = new TranslateTransform(),
image = image,
filename = c.ImageContent.filename,
X = c.ImageContent.X,
Y = c.ImageContent.Y,
iniX = c.ImageContent.iniX,
iniY = c.ImageContent.iniY,
};

Canvas.SetLeft(image, c.ImageContent.iniX);
Canvas.SetTop(image, c.ImageContent.iniY);
canvas.Children.Add(image);
c.ImageContent = im;
}
}
}


#region Manipulation Mode
private void InitImageManipulationTransforms(UIElement l)
{
_transformGroup = new TransformGroup();
_compositeTransform = new CompositeTransform();
_previousTransform = new MatrixTransform() { Matrix = Matrix.Identity };

_transformGroup.Children.Add(_previousTransform);
_transformGroup.Children.Add(_compositeTransform);

l.RenderTransform = _transformGroup;
}

void Image_ManipulationStarting(object sender, ManipulationStartingRoutedEventArgs e)
{
Image elem = sender as Image;
auxImage = elem;
Canvas cv = elem.Parent as Canvas;

foreach (ImageMath aux in listImage)
if (aux.image.Equals(elem))
{
double Width = aux.image.Width + 2;
double Height = aux.image.Height + 2;

Line l1 = new Line()
{
X1 = 0,
Y1 = 0,
X2 = (double)Width,
Y2 = 0,
StrokeThickness = 1,
Stroke = new SolidColorBrush(Colors.Black),
StrokeDashArray = new DoubleCollection() { 3 },
};
Line l2 = new Line()
{
X1 = 0,
Y1 = (double)Height,
X2 = (double)Width,
Y2 = (double)Height,
StrokeThickness = 1,
Stroke = new SolidColorBrush(Colors.Black),
StrokeDashArray = new DoubleCollection() { 3 },
};
Line l3 = new Line()
{
X1 = 0,
Y1 = 0,
X2 = 0,
Y2 = (double)Height,
StrokeThickness = 1,
Stroke = new SolidColorBrush(Colors.Black),
StrokeDashArray = new DoubleCollection() { 3 },
};
Line l4 = new Line()
{
X1 = (double)Width,
Y1 = 0,
X2 = (double)Width,
Y2 = (double)Height,
StrokeThickness = 1,
Stroke = new SolidColorBrush(Colors.Black),
StrokeDashArray = new DoubleCollection() { 3 },
};

if (aux.borderImage == null)
aux.borderImage = new List<Line>();
aux.borderImage.Add(l1);
aux.borderImage.Add(l2);
aux.borderImage.Add(l3);
aux.borderImage.Add(l4);
double X = aux.X;
double Y = aux.Y;
Canvas.SetLeft(l1, X);
Canvas.SetTop(l1, Y);
Canvas.SetLeft(l2, X);
Canvas.SetTop(l2, Y);
Canvas.SetLeft(l3, X);
Canvas.SetTop(l3, Y);
Canvas.SetLeft(l4, X);
Canvas.SetTop(l4, Y);
cv.Children.Add(l1);
cv.Children.Add(l2);
cv.Children.Add(l3);
cv.Children.Add(l4);
}
forceManipulationsToEnd = false;
e.Handled = true;
}

void Image_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
{
e.Handled = true;
}

void Image_ManipulationInertiaStarting(object sender, ManipulationInertiaStartingRoutedEventArgs e)
{
e.Handled = true;
}

void Image_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
if (forceManipulationsToEnd)
{
e.Complete();
return;
}

Image elem = sender as Image;
foreach (ImageMath aux in listImage)
if (aux.image.Equals(auxImage) && auxImage.Equals(elem))
{
dragTranslation = aux.dragTranslation;
for (int i = 0; i < aux.borderImage.Count; i++)
{
aux.borderImage.X1 += e.Delta.Translation.X;
aux.borderImage.X2 += e.Delta.Translation.X;
aux.borderImage.Y1 += e.Delta.Translation.Y;
aux.borderImage.Y2 += e.Delta.Translation.Y;
}
}

dragTranslation.X += e.Delta.Translation.X;
dragTranslation.Y += e.Delta.Translation.Y;

_previousTransform.Matrix = _transformGroup.Value;

Point center = _previousTransform.TransformPoint(new Point(e.Position.X, e.Position.Y));
_compositeTransform.CenterX = center.X;
_compositeTransform.CenterY = center.Y;

_compositeTransform.Rotation = e.Delta.Rotation;
_compositeTransform.ScaleX = _compositeTransform.ScaleY = e.Delta.Scale;
_compositeTransform.TranslateX = e.Delta.Translation.X;
_compositeTransform.TranslateY = e.Delta.Translation.Y;

e.Handled = true;
}

void Image_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
Image elem = sender as Image;
Canvas canvasParent = elem.Parent as Canvas;

foreach (ImageMath aux in listImage)
if (aux.image.Equals(elem))
{
aux.X = aux.iniX + dragTranslation.X;
aux.Y = aux.iniY + dragTranslation.Y;
aux.dragTranslation = dragTranslation;
elem.RenderTransform = this.dragTranslation;
if (aux.borderImage != null)
{
foreach (Line l in aux.borderImage)
canvasParent.Children.Remove(l);
aux.borderImage.Clear();
}
}
e.Handled = true;
}
#endregion

Continue reading...
 
Back
Top