C#打印屏幕预定义区域

时间:2011-03-19 11:04:14

标签: c# printing screen region

我想只捕获应用程序的预定义区域,例如我想仅打印屏幕组框。我将this.bounds更改为groupbox.bounds但它不起作用。它捕获其他区域但不捕获组框。有任何想法吗? 代码是:

// Set the bitmap object to the size of the screen
    bmpScreenshot = new Bitmap(this.Bounds.Width, this.Bounds.Height, PixelFormat.Format32bppArgb);
    // Create a graphics object from the bitmap
    gfxScreenshot = Graphics.FromImage(bmpScreenshot);
    // Take the screenshot from the upper left corner to the right bottom corner
    gfxScreenshot.CopyFromScreen(this.Bounds.X, this.Bounds.Y, 0, 0, this.Bounds.Size, CopyPixelOperation.SourceCopy);

SaveFileDialog saveImageDialog = new SaveFileDialog();
saveImageDialog.Title = "Select output file:";
saveImageDialog.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
//saveImageDialog.FileName = printFileName;
if (saveImageDialog.ShowDialog() == DialogResult.OK)
{enter code here
    // Save the screenshot to the specified path that the user has chosen
    bmpScreenshot.Save(saveImageDialog.FileName, ImageFormat.Png);
}

感谢。

1 个答案:

答案 0 :(得分:2)

问题是Bounds属性中的坐标是相对于其父坐标的,但CopyFromScreen需要绝对坐标。

尝试使用PointToScreen方法:

Point p = this.PointToScreen(new Point(groupBox.Bounds.X, groupBox.Bounds.Y));
gfxScreenshot.CopyFromScreen(p.X, p.Y, 0, 0, 
                 groupBox.Bounds.Size, CopyPixelOperation.SourceCopy);