[英]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屬性,您可以使用ActualWidth
和ActualHeight
。 同樣,它也不會公開Location
但您可以使用Left
和Top
屬性。
以上所有屬性都是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.