private Canvas trasformaCanvasDefinitivo() { BitmapSource bmpSource = (BitmapSource)imgMaschera.Source; Int32 newWidth = (int)((double)bmpSource.PixelWidth * 96d / bmpSource.DpiX); Int32 newHeight = (int)((double)bmpSource.PixelHeight * 96d / bmpSource.DpiY); Canvas c = new Canvas(); c.Background = new SolidColorBrush(Colors.Transparent); foreach (Visual visual in MyCanvas.Children) { Image fotina = (Image)visual; Image fotona = new Image(); fotona.Source = fotina.Source.Clone(); // Clono l'immagine iniziale della foto Rect rectFotina = new Rect(Canvas.GetLeft(fotina), Canvas.GetTop(fotina), fotina.ActualWidth, fotina.ActualHeight); Size sizeFondo = new Size(imgMaschera.ActualWidth, imgMaschera.ActualHeight); Size sizeMaschera = new Size(newWidth, newHeight); Rect newRect = Geometrie.proporziona(rectFotina, sizeFondo, sizeMaschera); fotona.Width = newRect.Width; fotona.Height = newRect.Height; c.Children.Add(fotona); // Imposto la posizione della foto all'interno del canvas della cornice. Canvas.SetLeft(fotona, newRect.Left); Canvas.SetTop(fotona, newRect.Top); // ---------------------------------------------- // --- ora mi occupo delle trasformazioni --- // ---------------------------------------------- if (fotina.RenderTransform is TransformGroup) { TransformGroup newTg = new TransformGroup(); TransformGroup tg = (TransformGroup)fotina.RenderTransform; foreach (Transform tx in tg.Children) { Debug.WriteLine("Trasformazione = " + tx.ToString()); if (tx is RotateTransform) { RotateTransform rtx = (RotateTransform)tx; // L'angolo rimane uguale che tanto è in gradi RotateTransform newTx = rtx.Clone(); // ricalcolo solo il punto centrale newTx.CenterX = rtx.CenterX * fotona.Width / fotina.ActualWidth; newTx.CenterY = rtx.CenterY * fotona.Height / fotina.ActualHeight; newTg.Children.Add(newTx); } else if (tx is TranslateTransform) { TranslateTransform ttx = (TranslateTransform)tx; // Devo riproporzionare le misure. // x1 : w1 = ? : w2 // ? = x1 * w2 / w1 double newX = ttx.X * fotona.Width / fotina.ActualWidth; double newY = ttx.Y * fotona.Height / fotina.ActualHeight; TranslateTransform newTx = new TranslateTransform(newX, newY); newTg.Children.Add(newTx); } else if (tx is MatrixTransform) { // Questo è il Flip MatrixTransform mtx = (MatrixTransform)tx; MatrixTransform newTx = (MatrixTransform)tx.Clone(); Matrix mMatrix = new Matrix(); mMatrix.Scale(-1.0, 1.0); mMatrix.OffsetX = mtx.Value.OffsetX * fotona.Width / fotina.ActualWidth; newTx.Matrix = mMatrix; newTg.Children.Add(newTx); } else if (tx is ScaleTransform) { ScaleTransform stx = (ScaleTransform)tx; // La scala rimane uguale perché è un fattore moltiplicativo. ScaleTransform newTx = stx.Clone(); // ricalcolo solo il punto centrale newTx.CenterX = stx.CenterX * fotona.Width / fotina.ActualWidth; newTx.CenterY = stx.CenterY * fotona.Height / fotina.ActualHeight; newTg.Children.Add(newTx); } } fotona.RenderTransform = newTg; } } // per concludere, aggiungo anche la maschera che deve ricoprire tutto. Image maschera = new Image(); maschera.Source = imgMaschera.Source.Clone(); maschera.Width = c.Width; maschera.Height = c.Height; c.Children.Add(maschera); // Non so se serve, ma dovrebbe provocare un ricalcolo delle posizioni dei componenti all'interno del canvas. c.InvalidateMeasure(); c.InvalidateArrange(); return(c); }
public ResizeAdorner(UIElement adornedElement, CustomStroke customStroke, CustomInkCanvas actualCanvas) : base(adornedElement) { if (customStroke is LinkStroke) { WIDTH_LEGER = 1; } adornedStroke = customStroke; visualChildren = new VisualCollection(this); if (customStroke is ShapeStroke) { strokeBounds = customStroke.GetCustomBound(); } else { strokeBounds = (customStroke as LinkStroke).GetStraightBounds(); } resizePreview = new Path(); resizePreview.Stroke = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFBBBBBB")); resizePreview.StrokeThickness = 1; visualChildren.Add(resizePreview); this.customStroke = customStroke; canvas = actualCanvas; Point center = customStroke.GetCenter(); if (customStroke is ShapeStroke) { rotation = new RotateTransform((customStroke as ShapeStroke).shapeStyle.rotation, center.X, center.Y); } else { rotation = new RotateTransform(0, center.X, center.Y); } while (rotation.Angle < 0) { rotation.Angle += 360; } outerBoundPath = new Path(); outerBoundPath.Stroke = Brushes.Black; outerBoundPath.StrokeDashArray = new DoubleCollection { 5, 2 }; outerBoundPath.StrokeThickness = 1; Rect rect = customStroke.GetCustomBound(); rect.X -= MARGIN; rect.Y -= MARGIN; rect.Width += MARGIN * 2; rect.Height += MARGIN * 2; outerBoundPath.Data = new RectangleGeometry(rect, 0, 0, rotation); visualChildren.Add(outerBoundPath); moveThumb = new Thumb(); moveThumb.Cursor = Cursors.SizeAll; moveThumb.Height = strokeBounds.Height + MARGIN * 2; moveThumb.Width = strokeBounds.Width + MARGIN * 2; moveThumb.Background = Brushes.Transparent; moveThumb.DragDelta += new DragDeltaEventHandler(Move_DragDelta); moveThumb.DragCompleted += new DragCompletedEventHandler(Move_DragCompleted); moveThumb.DragStarted += new DragStartedEventHandler(All_DragStarted); moveThumb.PreviewMouseUp += new MouseButtonEventHandler(LeftMouseUp); TransformGroup transform = new TransformGroup(); transform.Children.Add(new RotateTransform(rotation.Angle, moveThumb.Width / 2, moveThumb.Height / 2)); transform.Children.Add(new TranslateTransform(-canvas.ActualWidth / 2 + strokeBounds.Width / 2 + strokeBounds.X, -canvas.ActualHeight / 2 + strokeBounds.Height / 2 + strokeBounds.Y)); moveThumb.RenderTransform = transform; visualChildren.Add(moveThumb); unitX = rotation.Value.Transform(unitX); unitY = rotation.Value.Transform(unitY); // RenderTransform = rotation; anchors = new List <Thumb>(); anchors.Add(new Thumb()); anchors.Add(new Thumb()); anchors.Add(new Thumb()); anchors.Add(new Thumb()); anchors.Add(new Thumb()); anchors.Add(new Thumb()); anchors.Add(new Thumb()); anchors.Add(new Thumb()); int index = 0; foreach (Thumb anchor in anchors) { anchor.Width = 8; anchor.Height = 8; anchor.Background = new LinearGradientBrush((Color)ColorConverter.ConvertFromString("#FFc8d4ea"), (Color)ColorConverter.ConvertFromString("#FF809dce"), 45); anchor.BorderBrush = Brushes.Black; if (rotation.Angle % 360 >= 360 - 45 / 2 || rotation.Angle % 360 <= 45 / 2) { switch (index) { case 0: anchor.DragDelta += new DragDeltaEventHandler(Top_DragDelta); anchor.Cursor = Cursors.SizeNS; break; case 1: anchor.DragDelta += new DragDeltaEventHandler(Right_DragDelta); anchor.Cursor = Cursors.SizeWE; break; case 2: anchor.DragDelta += new DragDeltaEventHandler(Bottom_DragDelta); anchor.Cursor = Cursors.SizeNS; break; case 3: anchor.DragDelta += new DragDeltaEventHandler(Left_DragDelta); anchor.Cursor = Cursors.SizeWE; break; case 4: anchor.DragDelta += new DragDeltaEventHandler(TopLeft_DragDelta); anchor.Cursor = Cursors.SizeNWSE; break; case 5: anchor.DragDelta += new DragDeltaEventHandler(TopRight_DragDelta); anchor.Cursor = Cursors.SizeNESW; break; case 6: anchor.DragDelta += new DragDeltaEventHandler(BottomLeft_DragDelta); anchor.Cursor = Cursors.SizeNESW; break; case 7: anchor.DragDelta += new DragDeltaEventHandler(BottomRight_DragDelta); anchor.Cursor = Cursors.SizeNWSE; break; default: break; } } else if (rotation.Angle % 360 > 45 / 2 && rotation.Angle % 360 <= 90 - 45 / 2) { switch (index) { case 0: anchor.DragDelta += new DragDeltaEventHandler(Top_DragDelta); anchor.Cursor = Cursors.SizeNESW; break; case 1: anchor.DragDelta += new DragDeltaEventHandler(Right_DragDelta); anchor.Cursor = Cursors.SizeNWSE; break; case 2: anchor.DragDelta += new DragDeltaEventHandler(Bottom_DragDelta); anchor.Cursor = Cursors.SizeNESW; break; case 3: anchor.DragDelta += new DragDeltaEventHandler(Left_DragDelta); anchor.Cursor = Cursors.SizeNWSE; break; case 4: anchor.DragDelta += new DragDeltaEventHandler(TopLeft_DragDelta); anchor.Cursor = Cursors.SizeNS; break; case 5: anchor.DragDelta += new DragDeltaEventHandler(TopRight_DragDelta); anchor.Cursor = Cursors.SizeWE; break; case 6: anchor.DragDelta += new DragDeltaEventHandler(BottomLeft_DragDelta); anchor.Cursor = Cursors.SizeWE; break; case 7: anchor.DragDelta += new DragDeltaEventHandler(BottomRight_DragDelta); anchor.Cursor = Cursors.SizeNS; break; default: break; } } else if (rotation.Angle % 360 > 90 - 45 / 2 && rotation.Angle % 360 <= 90 + 45 / 2) { switch (index) { case 0: anchor.DragDelta += new DragDeltaEventHandler(Top_DragDelta); anchor.Cursor = Cursors.SizeWE; break; case 1: anchor.DragDelta += new DragDeltaEventHandler(Right_DragDelta); anchor.Cursor = Cursors.SizeNS; break; case 2: anchor.DragDelta += new DragDeltaEventHandler(Bottom_DragDelta); anchor.Cursor = Cursors.SizeWE; break; case 3: anchor.DragDelta += new DragDeltaEventHandler(Left_DragDelta); anchor.Cursor = Cursors.SizeNS; break; case 4: anchor.DragDelta += new DragDeltaEventHandler(TopLeft_DragDelta); anchor.Cursor = Cursors.SizeNESW; break; case 5: anchor.DragDelta += new DragDeltaEventHandler(TopRight_DragDelta); anchor.Cursor = Cursors.SizeNWSE; break; case 6: anchor.DragDelta += new DragDeltaEventHandler(BottomLeft_DragDelta); anchor.Cursor = Cursors.SizeNWSE; break; case 7: anchor.DragDelta += new DragDeltaEventHandler(BottomRight_DragDelta); anchor.Cursor = Cursors.SizeNESW; break; default: break; } } else if (rotation.Angle % 360 > 90 + 45 / 2 && rotation.Angle % 360 <= 135 + 45 / 2) { switch (index) { case 0: anchor.DragDelta += new DragDeltaEventHandler(Top_DragDelta); anchor.Cursor = Cursors.SizeNWSE; break; case 1: anchor.DragDelta += new DragDeltaEventHandler(Right_DragDelta); anchor.Cursor = Cursors.SizeNESW; break; case 2: anchor.DragDelta += new DragDeltaEventHandler(Bottom_DragDelta); anchor.Cursor = Cursors.SizeNWSE; break; case 3: anchor.DragDelta += new DragDeltaEventHandler(Left_DragDelta); anchor.Cursor = Cursors.SizeNESW; break; case 4: anchor.DragDelta += new DragDeltaEventHandler(TopLeft_DragDelta); anchor.Cursor = Cursors.SizeWE; break; case 5: anchor.DragDelta += new DragDeltaEventHandler(TopRight_DragDelta); anchor.Cursor = Cursors.SizeNS; break; case 6: anchor.DragDelta += new DragDeltaEventHandler(BottomLeft_DragDelta); anchor.Cursor = Cursors.SizeNS; break; case 7: anchor.DragDelta += new DragDeltaEventHandler(BottomRight_DragDelta); anchor.Cursor = Cursors.SizeWE; break; default: break; } } else if (rotation.Angle % 360 > 180 - 45 / 2 && rotation.Angle % 360 <= 180 + 45 / 2) { switch (index) { case 0: anchor.DragDelta += new DragDeltaEventHandler(Top_DragDelta); anchor.Cursor = Cursors.SizeNS; break; case 1: anchor.DragDelta += new DragDeltaEventHandler(Right_DragDelta); anchor.Cursor = Cursors.SizeWE; break; case 2: anchor.DragDelta += new DragDeltaEventHandler(Bottom_DragDelta); anchor.Cursor = Cursors.SizeNS; break; case 3: anchor.DragDelta += new DragDeltaEventHandler(Left_DragDelta); anchor.Cursor = Cursors.SizeWE; break; case 4: anchor.DragDelta += new DragDeltaEventHandler(TopLeft_DragDelta); anchor.Cursor = Cursors.SizeNWSE; break; case 5: anchor.DragDelta += new DragDeltaEventHandler(TopRight_DragDelta); anchor.Cursor = Cursors.SizeNESW; break; case 6: anchor.DragDelta += new DragDeltaEventHandler(BottomLeft_DragDelta); anchor.Cursor = Cursors.SizeNESW; break; case 7: anchor.DragDelta += new DragDeltaEventHandler(BottomRight_DragDelta); anchor.Cursor = Cursors.SizeNWSE; break; default: break; } } else if (rotation.Angle % 360 > 225 - 45 / 2 && rotation.Angle % 360 <= 225 + 45 / 2) { switch (index) { case 0: anchor.DragDelta += new DragDeltaEventHandler(Top_DragDelta); anchor.Cursor = Cursors.SizeNESW; break; case 1: anchor.DragDelta += new DragDeltaEventHandler(Right_DragDelta); anchor.Cursor = Cursors.SizeNWSE; break; case 2: anchor.DragDelta += new DragDeltaEventHandler(Bottom_DragDelta); anchor.Cursor = Cursors.SizeNESW; break; case 3: anchor.DragDelta += new DragDeltaEventHandler(Left_DragDelta); anchor.Cursor = Cursors.SizeNWSE; break; case 4: anchor.DragDelta += new DragDeltaEventHandler(TopLeft_DragDelta); anchor.Cursor = Cursors.SizeNS; break; case 5: anchor.DragDelta += new DragDeltaEventHandler(TopRight_DragDelta); anchor.Cursor = Cursors.SizeWE; break; case 6: anchor.DragDelta += new DragDeltaEventHandler(BottomLeft_DragDelta); anchor.Cursor = Cursors.SizeWE; break; case 7: anchor.DragDelta += new DragDeltaEventHandler(BottomRight_DragDelta); anchor.Cursor = Cursors.SizeNS; break; default: break; } } else if (rotation.Angle % 360 > 270 - 45 / 2 && rotation.Angle % 360 <= 270 + 45 / 2) { switch (index) { case 0: anchor.DragDelta += new DragDeltaEventHandler(Top_DragDelta); anchor.Cursor = Cursors.SizeWE; break; case 1: anchor.DragDelta += new DragDeltaEventHandler(Right_DragDelta); anchor.Cursor = Cursors.SizeNS; break; case 2: anchor.DragDelta += new DragDeltaEventHandler(Bottom_DragDelta); anchor.Cursor = Cursors.SizeWE; break; case 3: anchor.DragDelta += new DragDeltaEventHandler(Left_DragDelta); anchor.Cursor = Cursors.SizeNS; break; case 4: anchor.DragDelta += new DragDeltaEventHandler(TopLeft_DragDelta); anchor.Cursor = Cursors.SizeNESW; break; case 5: anchor.DragDelta += new DragDeltaEventHandler(TopRight_DragDelta); anchor.Cursor = Cursors.SizeNWSE; break; case 6: anchor.DragDelta += new DragDeltaEventHandler(BottomLeft_DragDelta); anchor.Cursor = Cursors.SizeNWSE; break; case 7: anchor.DragDelta += new DragDeltaEventHandler(BottomRight_DragDelta); anchor.Cursor = Cursors.SizeNESW; break; default: break; } } else { switch (index) { case 0: anchor.DragDelta += new DragDeltaEventHandler(Top_DragDelta); anchor.Cursor = Cursors.SizeNWSE; break; case 1: anchor.DragDelta += new DragDeltaEventHandler(Right_DragDelta); anchor.Cursor = Cursors.SizeNESW; break; case 2: anchor.DragDelta += new DragDeltaEventHandler(Bottom_DragDelta); anchor.Cursor = Cursors.SizeNWSE; break; case 3: anchor.DragDelta += new DragDeltaEventHandler(Left_DragDelta); anchor.Cursor = Cursors.SizeNESW; break; case 4: anchor.DragDelta += new DragDeltaEventHandler(TopLeft_DragDelta); anchor.Cursor = Cursors.SizeWE; break; case 5: anchor.DragDelta += new DragDeltaEventHandler(TopRight_DragDelta); anchor.Cursor = Cursors.SizeNS; break; case 6: anchor.DragDelta += new DragDeltaEventHandler(BottomLeft_DragDelta); anchor.Cursor = Cursors.SizeNS; break; case 7: anchor.DragDelta += new DragDeltaEventHandler(BottomRight_DragDelta); anchor.Cursor = Cursors.SizeWE; break; default: break; } } anchor.DragStarted += new DragStartedEventHandler(All_DragStarted); anchor.DragCompleted += new DragCompletedEventHandler(All_DragCompleted); if (!(customStroke is LinkStroke) || !(customStroke as LinkStroke).isAttached()) { visualChildren.Add(anchor); } double xOffset = 0; double yOffset = 0; switch (index) { case 0: //Top xOffset = strokeBounds.Width / 2; yOffset = -MARGIN; break; case 1: //Right xOffset = strokeBounds.Width + MARGIN; yOffset = strokeBounds.Height / 2; break; case 2: //Bottom xOffset = strokeBounds.Width / 2; yOffset = strokeBounds.Height + MARGIN; break; case 3: //Left xOffset = -MARGIN; yOffset = strokeBounds.Height / 2; break; case 4: //TopLeft xOffset = -MARGIN; yOffset = -MARGIN; break; case 5: //TopRight xOffset = strokeBounds.Width + MARGIN; yOffset = -MARGIN; break; case 6: //BottomLeft xOffset = -MARGIN; yOffset = strokeBounds.Height + MARGIN; break; case 7: //BottomRight xOffset = strokeBounds.Width + MARGIN; yOffset = strokeBounds.Height + MARGIN; break; default: break; } ArrangeAnchor(anchor, xOffset, yOffset); index++; } rotationPreview = rotation.Clone(); }