如何在WPF c# 4.0中构建一个放大镜

本文关键字:放大镜 一个 构建 WPF | 更新日期: 2023-09-27 18:02:09

我有一个渲染大的图像,需要缩放用户光标下的部分

我的图像是在一个网格中,并在XAML中定义如下:

<Grid x:Name="RootImgGrid" Background="#FF333333" >
    <Viewbox x:Name="imgViewBox"  Margin="1,1,1,1" Stretch="Fill" >
        <Canvas x:Name="imgCanvas"  ClipToBounds="True" Width="{Binding ElementName=RootImgGrid, Path=ActualWidth}" Height="{Binding ElementName=RootImgGrid, Path=ActualHeight}">
            <Image x:Name="imgObj" MouseWheel="img_MouseWheel" Cursor="Hand" MouseMove="Img_MouseMove" MouseDown="Img_MouseDown" MouseUp="Img_MouseUp"  >
                <Image.RenderTransform>
                    <TransformGroup x:Name="imgTransformGroup">
                        <ScaleTransform x:Name="imgScaleTransform"></ScaleTransform>
                        <TranslateTransform x:Name="imgTranslateTransform"></TranslateTransform>
                    </TransformGroup>
                </Image.RenderTransform>
                <Image.LayoutTransform>
                    <RotateTransform x:Name="imgRotateTransform"></RotateTransform>
                </Image.LayoutTransform>
            </Image>
        </Canvas>
    </Viewbox>
</Grid>

我有一个缩放区域光标下的图像应该缩放了2倍

<Viewbox x:Name="imgViewBoxMagnifier" Width="400" Height="90">
    <Canvas  x:Name="imgCanvasMagnifier" Width="400" Height="90">
        <Canvas.Clip>
            <RectangleGeometry Rect="0,0,400,90" />
        </Canvas.Clip>
        <Image  x:Name="imgMagnifier" Margin="2" Width="400" Height="90">
            <Image.RenderTransform>
                <TransformGroup x:Name="imgMagnifierTransformGroup">
                    <ScaleTransform x:Name="imgMagnifierScaleTransform"></ScaleTransform>
                    <TranslateTransform x:Name="imgMagnifierTranslateTransform"></TranslateTransform>
                </TransformGroup>
            </Image.RenderTransform>
            <Image.LayoutTransform>
                <RotateTransform x:Name="imgMagnifierRotateTransform"></RotateTransform>
            </Image.LayoutTransform>
        </Image>
    </Canvas>
</Viewbox>

现在,在我的。cs代码中,我已经有了应该显示缩放图像的函数:

public void Magnifier(Canvas imgCanvas, Image imgObject, Image imgMagnifier, MouseEventArgs e)
    {
        Int32 width = 400;
        Int32 height = 90;
        if (imgMagnifier.Source != imgObject.Source)
        {
            imgMagnifier.Source = imgObject.Source;
        }
        Size size = imgObject.RenderSize;
        RotateTransform rt = (RotateTransform)imgObject.LayoutTransform;
        TranslateTransform tt = (TranslateTransform)((TransformGroup)imgObject.RenderTransform).Children[1];
        ScaleTransform st = (ScaleTransform)((System.Windows.Media.TransformGroup)(imgObject.RenderTransform)).Children[0];
        Double x = e.GetPosition(imgCanvas).X - tt.X;
        Double y = e.GetPosition(imgCanvas).Y - tt.Y;
        Point pos = e.MouseDevice.GetPosition(imgCanvas);
        TransformGroup transformGroup = new TransformGroup();
        ScaleTransform scale = new ScaleTransform();
        scale.CenterX = x;
        scale.CenterY = y;
        scale.ScaleX = st.ScaleX * 10;
        scale.ScaleY = st.ScaleY * 10;
        transformGroup.Children.Add(scale);
        TranslateTransform translate = new TranslateTransform();
        translate.X = ??????????????????;
        translate.Y = ??????????????????;
        transformGroup.Children.Add(translate);
        imgMagnifier.RenderTransform = transformGroup;
    }

我如何找到那些"???????? . net"的正确值"

translate.X = -x * 10 / (size.Width / 400);
translate.Y = -y * 10 / (size.Height / 90);

它显示了正确的图像,放大了,但是它没有显示光标下的图像区域。

如何在WPF c# 4.0中构建一个放大镜

由于我没有找到任何打包的解决方案,我从零开始开发了一个功能齐全的Microsoft VS 2010 Project Magnifier Zoom c# 4.0 WPF。

它包括放大,缩小,旋转,适合宽度和放大区域。

请随意下载,并使其更好。

WPF放大器c# 4.0

希望你喜欢。如果你觉得有用,请投票。

public void Magnifier(Canvas imgCanvas, Image imgObject, Canvas imgCanvasMagnifier, Image imgMagnifier, MouseEventArgs e)
    {
        Double width = imgCanvasMagnifier.Width;
        Double height = imgCanvasMagnifier.Height;
        Int32 zoom = 3;
        String txtDebug = String.Empty;
        String txtZoom = String.Empty;
        if (imgMagnifier.Source != imgObject.Source)
        {
            imgMagnifier.Source = imgObject.Source;
        }
        Size size = imgObject.RenderSize;
        RotateTransform rt = (RotateTransform)imgObject.LayoutTransform;
        TranslateTransform tt = (TranslateTransform)((TransformGroup)imgObject.RenderTransform).Children[1];
        ScaleTransform st = (ScaleTransform)((System.Windows.Media.TransformGroup)(imgObject.RenderTransform)).Children[0];
        Double x = e.GetPosition(imgCanvas).X - tt.X;
        Double y = e.GetPosition(imgCanvas).Y - tt.Y;
        Point pos = e.MouseDevice.GetPosition(imgCanvas);
        var wnd = Canvas.GetTop(imgObject);
        TransformGroup transformGroup = new TransformGroup();
        ScaleTransform scale = new ScaleTransform();
        scale.ScaleX = st.ScaleX * zoom;
        scale.ScaleY = st.ScaleY * zoom;
        RotateTransform rotate = new RotateTransform();
        rotate.Angle = rt.Angle;
        TranslateTransform translate = new TranslateTransform();
        Double centerX = st.CenterX * (st.ScaleX - 1);
        Double centerY = st.CenterY * (st.ScaleY - 1);
        if (rt.Angle == 0)
        {
            translate.X = -(x + centerX) / st.ScaleX;
            translate.Y = -(y + centerY) / st.ScaleY;
            scale.CenterX = (x + centerX) / st.ScaleX;
            scale.CenterY = (y + centerY) / st.ScaleY;
        }
        if (rt.Angle == 90)
        {
            translate.X = -(x + centerX) / st.ScaleX;
            translate.Y = -(y + centerY) / st.ScaleY;
            translate.X += imgObject.ActualHeight * st.ScaleX * zoom;
            scale.CenterX = (x + centerX) / st.ScaleX;
            scale.CenterY = (y + centerY) / st.ScaleY;
        }
        if (rt.Angle == 180)
        {
            translate.X = -(x + centerX) / st.ScaleX;
            translate.Y = -(y + centerY) / st.ScaleY;
            translate.X += imgObject.ActualWidth * st.ScaleX * zoom;
            translate.Y += imgObject.ActualHeight * st.ScaleY * zoom;
            scale.CenterX = (x + centerX) / st.ScaleX;
            scale.CenterY = (y + centerY) / st.ScaleY;
        }
        if (rt.Angle == 270)
        {
            translate.X = -(x + centerX) / st.ScaleX;
            translate.Y = -(y + centerY) / st.ScaleY;
            translate.Y += imgObject.ActualWidth * st.ScaleX * zoom;
            scale.CenterX = (x + centerX) / st.ScaleX;
            scale.CenterY = (y + centerY) / st.ScaleY;
        }
        translate.X += width / 2;
        translate.Y += height / 2;
        transformGroup.Children.Add(rotate);
        transformGroup.Children.Add(scale);
        transformGroup.Children.Add(translate);

        imgMagnifier.RenderTransform = transformGroup;
    }

仅供参考:我从未使用过WPF,我利用这个机会学习。这是我的解决方案:

首先,我禁用了两个图像的拉伸:Stretch="None",并删除了图像的高度和宽度定义。

然后,后面的代码是:
    double xFactor = 2, yFactor = 2;
    Size size = imgObject.RenderSize;
    Size magSize = new Size(width: imgMagnifier.RenderSize.Width * xFactor,
                            height: imgMagnifier.RenderSize.Height * yFactor);
    Point pos = e.MouseDevice.GetPosition(imgCanvas);
    var transformGroup = new TransformGroup();
    var scale = new ScaleTransform();
    scale.CenterX = 0;
    scale.CenterY = 0;
    scale.ScaleX = xFactor;
    scale.ScaleY = yFactor;
    transformGroup.Children.Add(scale);
    var translate = new TranslateTransform();
    translate.X = -pos.X * xFactor + imgCanvasMagnifier.Width / 2;
    translate.Y = -pos.Y * yFactor + imgCanvasMagnifier.Height / 2;
    transformGroup.Children.Add(translate);
    imgMagnifier.RenderTransform = transformGroup;