第二个屏幕截图

12 c# screenshot winforms

嗨,我正在制作一个程序,用户可以截取屏幕截图.用户可以选择是否要从屏幕1,2,3或4中截取屏幕截图.我知道如何从第一个屏幕中获取第一个屏幕截图,但如何从屏幕2,3和4获取图像?

从第一个屏幕获取屏幕截图的代码如下所示:

 private void btnScreenOne_Click(object sender, EventArgs e) 
 {
     Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
         Screen.PrimaryScreen.Bounds.Height);

     Graphics graphics = Graphics.FromImage(bitmap as Image);

     graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);

     bitmap.Save(@"C:\Users\kraqr\Documents\PrintScreens\" + 
        DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + " Screen1" + 
        ".bmp", ImageFormat.Bmp);

}
Run Code Online (Sandbox Code Playgroud)

感谢答案.

PVi*_*itt 12

请改用Screen.AllScreens:

foreach ( Screen screen in Screen.AllScreens )
{
    screenshot = new Bitmap( screen.Bounds.Width,
        screen.Bounds.Height,
        System.Drawing.Imaging.PixelFormat.Format32bppArgb );
    // Create a graphics object from the bitmap
    gfxScreenshot = Graphics.FromImage( screenshot );
    // Take the screenshot from the upper left corner to the right bottom corner
    gfxScreenshot.CopyFromScreen(
        screen.Bounds.X,
        screen.Bounds.Y, 
        0, 
        0,
        screen.Bounds.Size,
        CopyPixelOperation.SourceCopy );
    // Save the screenshot
}
Run Code Online (Sandbox Code Playgroud)

  • 可以在不使用 System.Windows.Forms 的情况下使用屏幕吗?我正在 WPF 中执行此操作,并且不想引用 System.Windows.Forms (2认同)

Joe*_*oey 9

Screen类有一个静态属性AllScreens,让你的屏幕的数组.那些物品有一个Bounds你肯定可以使用的属性......

简而言之:您使用所需屏幕的大小初始化位图(不要使用PrimaryScreen,因为这只是主要的一个,顾名思义),然后传递适当的边界CopyFromScreen.

  • 我的通灵能力告诉我,您正在“(0, 0)”位置截取屏幕截图,而不是第二个屏幕实际开始的位置。请参阅 [PVitt'answer](http://stackoverflow.com/questions/6175968/screenshot-from-second-screen/6176097#6176097) 了解应该有效的代码。 (2认同)

Dan*_*rth 6

用于Screen.AllScreens通过Bounds特定屏幕的属性检索坐标并将其传递给CopyFromScreen.