C# 打印屏幕预定义区域
我只想捕获应用程序的预定义区域,例如我只想打印屏幕组框。我将 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);
}
谢谢。
I want to capture only a predefined region of the application, for example I want to print screen only the group box. I changed this.bounds to groupbox.bounds but it doesn't work. It captures other regions but not the group box. Any ideas?
The codes are:
// 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);
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于
Bounds
属性中的坐标是相对于其父级的,但CopyFromScreen
需要绝对坐标。尝试使用 PointToScreen 方法:
The problem is that the coordinates in the
Bounds
property are relative to its parent, butCopyFromScreen
needs absolute coordinates.Try using the PointToScreen method: