如何在WPF C#4.0中构建放大镜?

前端之家收集整理的这篇文章主要介绍了如何在WPF C#4.0中构建放大镜?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个渲染大的图像,需要缩放用户光标下的部分.

我的图像在Grid中,并以这种方式在XAML中定义:

<Grid x:Name="RootImgGrid" Background="#FF333333" >
    <ViewBox x:Name="imgViewBox"  Margin="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,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;

    }

我怎样才能找到那些“????????”的正确值

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

显示正确的图像,缩放,但它不显示光标下的图像区域.

提前致谢.

解决方法

由于我没有找到任何打包解决方案,我从头开发了一个功能齐全的Microsoft VS 2010项目放大镜Zoom C#4.0 WPF.

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

随意下载,并使其更好.

WPF Magnifier C# 4.0

希望你喜欢.如果你发现它有用,请投票.

public void Magnifier(Canvas imgCanvas,Canvas imgCanvasMagnifier,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;

    }
原文链接:https://www.f2er.com/csharp/243606.html

猜你在找的C#相关文章