C# 打印屏幕预定义区域

发布于 2024-10-22 22:23:46 字数 1036 浏览 1 评论 0原文

我只想捕获应用程序的预定义区域,例如我只想打印屏幕组框。我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两相知 2024-10-29 22:23:46

问题在于 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);

The problem is that the coordinates in the Bounds property are relative to its parent, but CopyFromScreen needs absolute coordinates.

Try using the PointToScreen method:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文