Screenshot of the main WPF window under the second monitor / TV

I use my application on the second monitor, and sometimes on the main computer monitor.

How can I get a screenshot of a second monitor?

The following code does not work for the second monitor ...

Graphics gfx; Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); gfx = Graphics.FromImage(bmp); gfx.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy); MemoryStream ms = new MemoryStream(); bmp.Save(ms, ImageFormat.Jpeg); byte[] bitmapData = ms.ToArray(); 
+2
source share
2 answers

Use Screen.AllScreens[1].Bounds isntead Screen.PrimaryScreen.Bounds .

Or it’s more reliable to get the first non-primary screen.

 var secondScreen = Screen.AllScreens.Where(screen => !screen.Primary).FirstOrDefault(); 

check secondScreen == null to see if you have a second screen.

Edit :
You may also be interested in Screen.FromControl , which displays the screen on which the application is currently running.

+3
source

This code does not work for your second screen because it explicitly uses Screen.PrimaryScreen .

If you want to explicitly display from the second screen (ignoring the case when you see 3 ... n), you can replace PrimaryScreen with AllScreens[1] .

Keep in mind that this will break if you ever turn off this second screen.

It looks like you want to capture the application window instead of the screen if the application does not occupy the entire screen or break two screens. WPF has this feature natively: Get the System.Drawing.Bitmap of a WPF area using VisualBrush

+1
source

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


All Articles