반응형

WPF에서 원활한 확대/축소를 달성하려면 마우스 움직임에 따라 이미지를 직접 업데이트하는 대신 애니메이션을 사용하여 이미지의 크기를 점진적으로 변경할 수 있습니다.

<Window x:Class="WpfImageZoom.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Image Zoom" Width="800" Height="600">
    <Grid>
        <Canvas Name="canvas">
            <Image Name="image" Source="your_image.jpg" Stretch="None" MouseWheel="OnMouseWheel" />
        </Canvas>
    </Grid>
</Window>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace WpfImageZoom
{
    public partial class MainWindow : Window
    {
        private double zoomScale = 1.0;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void OnMouseWheel(object sender, MouseWheelEventArgs e)
        {
            // Calculate the zoom scale factor (adjust as needed)
            double deltaZoom = e.Delta > 0 ? 0.1 : -0.1;

            // Apply a smooth zoom animation
            DoubleAnimation animation = new DoubleAnimation(zoomScale, zoomScale + deltaZoom, new Duration(TimeSpan.FromMilliseconds(200)));
            animation.AccelerationRatio = 0.2;
            animation.DecelerationRatio = 0.8;

            // Update the zoom scale and apply the animation to the image's scale
            zoomScale += deltaZoom;
            ScaleTransform scale = new ScaleTransform(zoomScale, zoomScale);
            image.RenderTransform = scale;

            // Begin the animation
            scale.BeginAnimation(ScaleTransform.ScaleXProperty, animation);
            scale.BeginAnimation(ScaleTransform.ScaleYProperty, animation);

            // Prevent the event from bubbling
            e.Handled = true;
        }
    }
}
반응형

+ Recent posts