簡體   English   中英

捕獲WPF中當前表單選擇的屏幕?

[英]Capture the screen selected by Current Form in WPF?

我正在創建一個應用程序,它可以捕獲當前活動窗體選擇的屏幕,並通過將其設置為背景來使用戶知道。 請參考圖片

在此輸入圖像描述

但我的問題是我無法獲得活動窗口大小的大小。 這是我一直在努力的代碼

 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'不包含'Size,location的定義。 但這在Windows窗體中運行良好。 任何幫助都很受歡迎。 謝謝。

WPF窗口沒有size屬性,您可以使用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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM