gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:24:22 25 4
gpt4 key购买 nike

我有一张渲染得很大的图像,需要放大用户光标下的部分。

我的图像位于网格内,并以这种方式在 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;

}

如何找到那些“?????????”的正确值

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

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

提前致谢。

最佳答案

因为我没有找到任何打包的解决方案,所以我从头开始开发了一个功能齐全的 Microsoft VS 2010 Project Magnifier Zoom C# 4.0 WPF。

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

请随意下载,并使它变得更好。

WPF Magnifier 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;

}

关于c# - 如何在 WPF C# 4.0 中构建放大镜?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6713853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com