Vilix
Vilix

Reputation: 101

How set different origin centers for scaletransform and rotatetransform?

How I can set different origin centers for scaletransform and rotatetransform?

example xaml:

<ScaleTransform ScaleX="{Binding Zoom}"
                                        ScaleY="{Binding Zoom}"
                                        CenterX="0"
                                        CenterY="0" />
<RotateTransform Angle="{Binding RotateAngle}"
                                         CenterX="0.5"
                                         CenterY="0.5"/>

All that comes to mind: is when you click on the zoom or rotation slider change RenderTransformOrigin via bindings. Is this right way? P.S.

Sorry for my eng.

Upvotes: 2

Views: 1192

Answers (2)

Vilix
Vilix

Reputation: 101

After H.B. answer I solve my problem. Maybe someone will be useful for my solution. Work like a charm for me: Rotate with origin=0.5,0.5; Scale like 0,0

XAML:

<ScrollViewer Grid.Row="1"
                      HorizontalScrollBarVisibility="Auto"
                      VerticalScrollBarVisibility="Auto">
            <Canvas x:Name="canvas"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    Width="{Binding CanvasW}"
                    Height="{Binding CanvasH}"
                    >
        <Image HorizontalAlignment="Center"
                       Stretch="None"
                       x:Name="image1"
                       VerticalAlignment="Center"
                       RenderTransformOrigin="{Binding TransformOrigin}">
                    <Image.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform ScaleX="{Binding Zoom}"
                                            ScaleY="{Binding Zoom}" />
                            <RotateTransform Angle="{Binding RotateAngle}" />
                            <TranslateTransform X="{Binding TransX}"
                                                Y="{Binding TransY}" />
                        </TransformGroup>
                    </Image.RenderTransform>
        </Image>
        </Canvas>
<ScrollViewer/>

ViewModel:

public double Zoom
        {
            get { return zoom; }
            set 
            {
                if (value != zoom)
                {
                    zoom = value;
                    RaisePropertyChanged("Zoom");
                    RaisePropertyChanged("CanvasH");
                    RaisePropertyChanged("CanvasW");
                    RaisePropertyChanged("TransX");
                    RaisePropertyChanged("TransY");
                }
            }
        }

public double TransX
        {
            get 
            {
                if (imageSource != null)
                {
                    return ((imageSource.Width *zoom - imageSource.Width)/2;
                }
                return 0;
            }
        }

public double TransY
        {
            get
            {
                if (imageSource != null)
                {
                    return (imageSource.Height * zoom - imageSource.Height) / 2;
                }
                return 0;
            }
        }
public double CanvasH
        {
            get 
            {
                if (imageSource!=null)
                {
                    return imageSource.Height*zoom;
                }
                return canvasH;
            }
}
public double CanvasW
        {
            get
            {
                if (imageSource != null)
                {
                    return imageSource.Width*zoom;
                }
                return canvasW;
            }

Upvotes: 1

brunnerh
brunnerh

Reputation: 185410

I would probably approach this by using TranslateTransforms to move the object before each operation. You can also use the Inverse of a transform to nullify it (e.g. move the object back to origin before the next operation).

Upvotes: 2

Related Questions