c# - 捕获当前窗体在 WPF 中选择的屏幕?

标签 c# wpf screen-capture

我正在创建一个应用程序,它可以捕获当前事件窗口窗体选择的屏幕,并通过将其设置为背景让用户知道。请引用图片

enter image description here

但我的问题是我无法获取事件窗口的大小。这是我一直在研究的代码

 private void button5_Click(object sender, RoutedEventArgs e)

    {
        Image b = null;
        int w = (int)this.Width;
        int h = (int)this.Height;
        **System.Drawing.Size sz = this.Size;
        System.Drawing.Point loc = this.Location;**
        Hide();
        System.Threading.Thread.Sleep(500);
        using (b = new Bitmap(w, h))
        {
            using (Graphics g = Graphics.FromImage(b))
            {
                g.CopyFromScreen(loc, new System.Drawing.Point(0, 0), sz);
            }

            Image x = new Bitmap(b);

            ImageBrush myBrush = new ImageBrush();
            x.Save(@"C:\Users\DZN\Desktop\testBMP.jpeg", ImageFormat.Jpeg);
            myBrush.ImageSource = new BitmapImage(new Uri(@"C:\Users\DZN\Desktop\testBMP.jpeg", UriKind.Absolute));
            this.Background = myBrush;

        }
        Show();
    }

在粗体行中,我收到错误提示 WpfApplication1.MainWindow' 不包含“大小、位置”的定义。但这在 Windows 窗体中运行良好。非常感谢任何帮助。谢谢你。

最佳答案

WPF 窗口没有大小属性​​,您可以使用 ActualWidthActualHeight。同样,它也不会公开 Location,但您可以使用 LeftTop 属性。

所有上述属性都是 double 类型,因此您需要转换为适当的类型。

System.Drawing.Size sz = new System.Drawing.Size((int)ActualWidth, (int)ActualHeight);
System.Drawing.Point loc = new System.Drawing.Point((int)Left, (int)Top);

关于c# - 捕获当前窗体在 WPF 中选择的屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22731969/

相关文章:

c# - 将对象列表从 VB6 编码到 C#

c# - 如何在 autofac 中混合装饰器?

c# - user.config 未在本地应用程序数据中生成

c# - 我怎样才能为表单中的任何 child 捕获 MouseLeftButtonDown 事件,即使 child 捕获了这个事件?

ios - 尝试镜像 Apple Watch 屏幕进行演示

android - 在 Amazon Kindle Fire HD 中通过 adb 截屏

c# - 基于语言的条件代码?

c# - Xamarin.Forms ListView 中的项目之间的填充

c# - 背景属性不指向路径 '(0).(1)' 中的依赖对象

iphone - UIWebView 的屏幕截图中视频黑屏