Exemplo n.º 1
0
        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);
        }