[英]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.