【问题标题】:Why window smaller than actualwidth?为什么窗口小于实际宽度?
【发布时间】:2019-04-11 05:40:57
【问题描述】:

我想要捕获窗口,但实际窗口大小似乎比图小。

这是代码

<Window x:Class="FileRead.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Width="620" Height="340" >
<Grid>

    <StackPanel>
        <StackPanel Orientation="Horizontal">
            <Button x:Name="ReadImageButton" Width="100" Height="30" Margin="10" Click="ReadImage_Click">
                LoadImage
            </Button>

            <Button x:Name="ReadTextButton" Width="100" Height="30" Margin="10" Click="ReadText_Click">
                LoadText
            </Button>

            <Button x:Name="CaptueScreenButton" Width="80" Height="30" Margin="10" Click="CaptueScreenButton_Click">
                ScreenCapture
            </Button>

            <Button x:Name="CaptuerWindowButton" Width="80" Height="30" Margin="10" Click="CaptuerWindowButton_Click">
                WindowCapture
            </Button>

我找不到问题。

private void CaptuerWindowButton_Click(object sender, RoutedEventArgs e)
{
    int width = (int)this.ActualWidth;
    int height = (int)this.ActualHeight;

    Point point = this.PointToScreen(new Point(0, 0)); 

    CheckLable.Content = string.Format("{0} / {1}", this.Width, this.ActualWidth);

    using (Bitmap bmp = new Bitmap(width, height))
    {
        using (Graphics gr = Graphics.FromImage(bmp))
        {
            gr.CopyFromScreen( (int)point.X, (int)this.Top, 0, 0, bmp.Size);

        }


        bmp.Save(ImagePath + "/WindowCapture.png", ImageFormat.Png);
    }
}

结果图片

总有15分左右的差距。:

请帮帮我。

enter image description here

【问题讨论】:

    标签: c# wpf window size


    【解决方案1】:

    您的问题的原因是窗口的大小包括操作系统绘制的区域,称为“非客户区”,通常包括框架,边框,下降显示效果。你的计算没有考虑到这一点。正确的代码会喜欢

        var clientTopLeft = this.PointToScreen(new System.Windows.Point(0, 0));
        // calculate the drop show effect offset. 
        var shadowOffset = SystemParameters.DropShadow ? clientTopLeft.X - Left - 
            ((WindowStyle == WindowStyle.None && ResizeMode < ResizeMode.CanResize) ? 0 : SystemParameters.BorderWidth) : 0;
    
        // exclude left and right drop shadow area
        int width = (int)(Width - 2 * shadowOffset);
        // exclude bottom drop shadow area
        int height = (int)(Height - shadowOffset);
    
        using (Bitmap bmp = new Bitmap(width, height))
        {
            using (Graphics gr = Graphics.FromImage(bmp))
            {
                gr.CopyFromScreen((int)(Left + shadowOffset),
                    (int)Top, 0, 0, bmp.Size);
            }
    
            bmp.Save("WindowCapture.png");
        }
    

    【讨论】:

    • 谢谢。解释也很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 2019-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多