WPF WebBrowser C# Windows 不会旋转渲染

WPF WebBrowser C# Windows won't rotate the rendering

这应该是一项简单的任务。我正在尝试创建一个 WPF 应用程序,它将旋转 WebBrowser 并在单击按钮时进行 Web 渲染。所以理想情况下,这就是我想要的:

原版浏览器:

旋转浏览器:

完成任务;以下是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TestClawCam
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        int height = 480;
        int width = 640;
        public MainWindow()
        {
            InitializeComponent();
            clawCam1.Source= new Uri("http://google.com");
            clawCam1.Width = width;
            clawCam1.Height = height;


        }

        private void rotateButton_Click(object sender, RoutedEventArgs e)
        {
            RotateTransform myTransform = new RotateTransform();
            myTransform.Angle = 90;
            clawCam1.RenderTransform = myTransform;

        }
    }
}

在 运行 之后,代码只有尺寸在变化,实际渲染没有变化。这是我得到的输出:

原版浏览器:

旋转浏览器:

如您所见,尺寸发生了变化,但实际网页并未发生变化。以下是我已经检查过的链接:

Rotating webbrowser from current orientation is stretching(zooming) the webbrowser contents Windows phone

Upside down browser in WPF application

Rotate Windows form upside down

此外,如果它是 label/image/canvas,它会旋转尺寸并似乎旋转了整个对象。任何帮助深表感谢。

P.S。系统配置:

Windows 7

Visual Studio 2012

.Net Framework 4.5

旋转浏览器看起来比想象中的更麻烦,所以实际上 LayoutTransform 和 RenderTransform 都不起作用,如果你需要旋转浏览器你需要使用 Awesomium

您尝试的方法行不通。 RenderTransform 是一个 WPF 概念,但 WebBrowser 控件不使用 WPF 渲染管道。在引擎盖下,它只是一个 Internet Explorer window。就像 WindowsFormsHost 一样,它在 WPF 显示顶部覆盖了一个 different window 对象,并且没有 WPF 属性 可以以任何方式影响其内容.

如果您想旋转网页内容,您必须说服 IE 这样做。它不会是完美的,但您应该能够非常接近,具体取决于您要显示的内容。我能想到的最简单的解决方案是使用 CSS 转换,因此我们将向页面注入样式表。

首先,您必须添加对 MSHTML(IE 的呈现引擎)的引用。这是一个 COM 参考:

然后,您必须处理 Navigated 事件:

clawCam1.Navigated += OnNavigated;

然后你在事件处理程序中做脏活累活:

private void OnNavigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
    var doc = clawCam1.Document as HTMLDocument;
    if (doc == null)
        return;

    var css = doc.createStyleSheet(string.Empty, 0);
    css.cssText = "body { transform: rotate(90deg); }";
}

这是我得到的结果: