我正在创建一个应用程序,它可以捕获当前事件窗口窗体选择的屏幕,并通过将其设置为背景让用户知道。请引用图片
但我的问题是我无法获取事件窗口的大小。这是我一直在研究的代码
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 窗口没有大小属性,您可以使用 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);
关于c# - 捕获当前窗体在 WPF 中选择的屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22731969/