首页 > 解决方案 > WPF中的旋转图像不考虑屏幕尺寸

问题描述

我目前在 WPF 中旋转图像时遇到问题,使用RotateTransformand LayoutTransform. 当图像的像素高度大于显示器高度并以 90º 或 270º 旋转时,窗口大小将高于显示器屏幕分辨率大小。

示例屏幕截图: 应用程序以 90º 的图像 运行 应用程序以 0º 的图像运行

我正在使用下面的代码(简化),其中mainWindow.imgSystem.Windows.Control.Image

static void Rotate(int degrees)
{
    var rt = new RotateTransform { Angle = degrees };
    mainWindow.img.LayoutTransform = rt;
}

这是一个图片查看器项目,完整的源代码在https://github.com/Ruben2776/PicView

我尝试改变图像的宽度和高度值,但它会产生不希望的结果(倾斜的比例)。

图像大小的大小计算是根据用户的屏幕高度进行的,使用以下修剪代码:

int interfaceHeight = 90;
double maxWidth = Math.Min(MonitorInfo.Width, width);
double maxHeight = Math.Min((MonitorInfo.Height - interfaceHeight), height);
double AspectRatio = Math.Min((maxWidth / width), (maxHeight / height));
mainWindow.img.Width = (width * AspectRatio);
mainWindow.img.Height = (height * AspectRatio);

和是图像的尺寸,height是检索当前显示器分辨率的类。widthMonitorInfo

更新

以下是说明该问题的示例 WPF 应用程序的最小代码:

主窗口.xaml

<Window x:Class="RotateTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        SizeToContent="WidthAndHeight"
        Title="MainWindow" >
    <Grid>
        <Image x:Name="img" Stretch="Fill" Source="https://w.wallhaven.cc/full/nk/wallhaven-nkrwz1.jpg"/>
    </Grid>
</Window>

主窗口.xaml.cs

using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace RotateTest
{
    public partial class MainWindow : Window
    {
        int Degrees;

        public MainWindow()
        {
            InitializeComponent();
            ContentRendered += MainWindow_ContentRendered;
            KeyDown += MainWindow_KeyDown;
        }

        private void MainWindow_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {                
                case Key.Up:
                    Rotate(true);
                    break;
                case Key.Down:
                    Rotate(false);
                    break;

            }
        }

        private void MainWindow_ContentRendered(object sender, EventArgs e)
        {
            int interfaceHeight = 90;
            double maxWidth = Math.Min(SystemParameters.PrimaryScreenWidth, img.Source.Width);
            double maxHeight = Math.Min(SystemParameters.PrimaryScreenHeight - interfaceHeight, img.Source.Height);
            double AspectRatio = Math.Min((maxWidth / img.Source.Width), (maxHeight / img.Source.Height));
            img.Width = (img.Source.Width * AspectRatio);
            img.Height = (img.Source.Height * AspectRatio);
        }

        void Rotate(int degrees)
        {
            var rt = new RotateTransform { Angle = Degrees = degrees };
            img.LayoutTransform = rt;
        }

        void Rotate(bool right)
        {
            switch (Degrees)
            {
                case 0:
                    if (right)
                    {
                        Rotate(270);
                    }
                    else
                    {
                        Rotate(90);
                    }

                    break;

                case 90:
                    if (right)
                    {
                        Rotate(0);
                    }
                    else
                    {
                        Rotate(180);
                    }

                    break;

                case 180:
                    if (right)
                    {
                        Rotate(90);
                    }
                    else
                    {
                        Rotate(270);
                    }

                    break;

                case 270:
                    if (right)
                    {
                        Rotate(180);
                    }
                    else
                    {
                        Rotate(0);
                    }

                    break;
            }
        }
    }
}

标签: c#wpfimage-rotation

解决方案


问题的根源在于,您只是在未旋转时根据图像的大小计算缩放比例。旋转图像后,img.ActualHeight有效地变为其宽度并img.ActualWidth有效地变为其高度,并且您从图像未旋转时的计算不再正确。

以下是我对您的代码所做的更改和添加:

private double normalRatio;
private double rotatedRatio;

private void MainWindow_ContentRendered(object sender, EventArgs e)
{
    double interfaceHeight = this.ActualHeight - img.ActualHeight;

    normalRatio = Math.Min(SystemParameters.WorkArea.Width / img.Source.Width, (SystemParameters.WorkArea.Height - interfaceHeight) / img.Source.Height);
    rotatedRatio = Math.Min(SystemParameters.WorkArea.Width / img.Source.Height, (SystemParameters.WorkArea.Height - interfaceHeight) / img.Source.Width);

    ScaleImage();
}

private void ScaleImage()
{
    double ratio = Degrees == 0 || Degrees == 180 ? normalRatio : rotatedRatio;
    img.Width = (img.Source.Width * ratio);
    img.Height = (img.Source.Height * ratio);
}

void Rotate(bool right)
{
    if (right)
    {
        Degrees -= 90;
        if (Degrees < 0) { Degrees += 360; }
    }
    else
    {
        Degrees += 90;
        if (Degrees >= 360) { Degrees -= 360; }
    }

    ScaleImage();
    Rotate(Degrees);
}

//I left the other methods, including Rotate(int degrees), the same as in your question

以下是对我所做更改的解释:

  • interfaceHeight通过从窗口的高度中减去图像的高度来计算,差值是其他所有内容的总大小。
  • MonitorInfo我没有使用 ,而是使用SystemParameters.WorkArea,因为它考虑了 Windows 任务栏的大小和位置。
  • 我计算了两个比例:normalRatio,当图像不旋转或垂直翻转(180°)时,以及rotatedRatio,当图像在任一方向旋转 90° 时。img.Source.Height我通过交换和来计算后者img.Source.Width
  • 我添加了一个ScaleImage()方法来根据预期的旋转进行实际的图像缩放,所以我可以从两个不同的地方调用它。
  • 我简化Rotate(bool right)了使用数学计算新角度,而不是列出每个可能的旋转。

上述结果在保持原始纵横比的同时始终使屏幕尽可能大的图像。当它旋转以适应屏幕时,它会增长和缩小。如果您希望图像保持恒定大小,只需使用Math.Min(normalRatio, rotatedRatio).

请注意,以上仅在您调用时有效Rotate(bool right)而不是在您Rotate(int degrees)直接调用时有效。这是因为使用两个比率的逻辑仅适用,因为图像只有两种可能的尺寸(纵向和横向),仅当您将旋转限制为 90° 增量时才会出现这种情况。如果您想将角度设置为其他值,例如 20°,计算图像有效尺寸的数学会变得有点复杂,您需要开始根据角度动态计算它。


推荐阅读