我用我的第二个显示器上,有时在电脑的主监视器应用程序。
我怎样才能得到第二个显示器的屏幕截图?
下面code没有第二个显示器工作...
显卡GFX;
BMP位图=新位图(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的毫秒=新的MemoryStream();
bmp.Save(MS,ImageFormat.Jpeg);
byte []的位图数据= ms.ToArray();
解决方案
使用 Screen.AllScreens [1] .Bounds
的isntead Screen.PrimaryScreen .Bounds
。
或者更可靠拿到的第一个非主屏幕。
VAR secondScreen = Screen.AllScreens.Where(屏幕=>!screen.Primary).FirstOrDefault();
检查 secondScreen == NULL
要知道,如果你有一个secondScreen。
修改:
您可能也有兴趣 Screen.FromControl
,让屏幕应用程序正在运行。
I use my application on the second monitor and sometimes at the primary monitor of the computer.
How I can get screenshot of the second monitor?
The following code doesn't 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();
解决方案
Use Screen.AllScreens[1].Bounds
isntead of Screen.PrimaryScreen.Bounds
.
Or more reliable to get the first non Primary Screen.
var secondScreen = Screen.AllScreens.Where(screen => !screen.Primary).FirstOrDefault();
check for secondScreen == null
to know if you have a secondScreen.
Edit:
You might also be interested in Screen.FromControl
that gives the screen that the application is currently running on.