Capturing the screen selected by the current form in WPF?

I am creating an application that can capture the screen that is selected by the current active window shape and inform the user by setting it as the background. Please refer to the image

enter image description here

But my problem is that I cannot get the size of the active window size. This is the code I worked 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 bold lines, I get an error: WpfApplication1.MainWindow 'does not contain a definition for "Size, location". but it works well in window forms. Any help is appreciated. Thank.

+4
source share
1 answer

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);
+4

Source: https://habr.com/ru/post/1534043/


All Articles