简体   繁体   中英

Capture the screen selected by Current Form in WPF?

I am creating a application which can capture the screen which is selected by current active windows form and make user aware by setting it as the background of it. Please refer the image

在此输入图像描述

But my problem is i cant get the size of the active window size. This is the code i have been working on

 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();
    }

In the bolded lines i get the error saying WpfApplication1.MainWindow' does not contain a definition for 'Size, location. but this works well in windows forms. Any help is hugly appreciated. Thank you.

WPF Window doesn't have a size property instead you can use ActualWidth and ActualHeight . Same way it doesn't exposes Location also but you can use Left and Top properties.

All the above properties are of type double so you need a cast to appropriate type.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM