If you want the following animation effect, you could try to refer to the following example. Even if you don't drag and resize canvas proportionally, the canvas remains in its original proportions.
MainWindow.xaml:
<Grid Background="AliceBlue">
<Canvas x:Name="cvs" Width="300" Height="300" MaxWidth="900" MaxHeight="900" Canvas.Left="0" Canvas.Top="0" MinWidth="300" MinHeight="300" >
<Canvas.Background>
<ImageBrush ImageSource="ges.png" Stretch="Uniform"/>
</Canvas.Background>
<TextBox Canvas.Left="0" Canvas.Top="0" Name="changes"
Width="200" Height="30" Background="Pink" Foreground="Black"
BorderThickness="0"/>
<Thumb Name="myThumb" Canvas.Right="0" Canvas.Bottom="0" Background="Blue"
Width="10" Height="10" DragDelta="onDragDelta"
DragStarted="onDragStarted" DragCompleted="onDragCompleted" />
</Canvas>
</Grid>
MainWindow.xaml.cs:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
void onDragDelta(object sender, DragDeltaEventArgs e)
{
double yadjust = cvs.Height + e.VerticalChange;
double xadjust = cvs.Width + e.VerticalChange;
if (yadjust>=0 && xadjust>=0)
{
cvs.Width = yadjust;
cvs.Height = yadjust;
Canvas.SetLeft(myThumb, Canvas.GetLeft(myThumb) +
e.HorizontalChange);
Canvas.SetTop(myThumb, Canvas.GetTop(myThumb) +
e.VerticalChange);
changes.Text = "Size: " +
cvs.Width.ToString(CultureInfo.InvariantCulture) +
", " +
cvs.Height.ToString(CultureInfo.InvariantCulture);
}
}
void onDragStarted(object sender, DragStartedEventArgs e)
{
myThumb.Background = Brushes.Orange;
}
void onDragCompleted(object sender, DragCompletedEventArgs e)
{
myThumb.Background = Brushes.Blue;
}
}
The result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.