c#活动窗口的屏幕截图但不捕获窗口 [英] c# screenshot of active window but doesn't caputre the window

查看:102
本文介绍了c#活动窗口的屏幕截图但不捕获窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,我需要在其中创建一个带有应用程序屏幕截图的 PDF 文件.

I'm creating an application in which I need to create a PDF file with the screenshot of the application.

我找到了如何创建屏幕截图以及如何将其放入我的文件中.在大多数情况下,一切都运行良好.

I found how to create the screenshot and how to put it in my file. All is working well in most situations.

当我使用多个屏幕或类似 Teamviewer 的程序时,我的问题就出现了.问题是,我的程序捕获了正确的区域(无论是哪个屏幕,屏幕上的良好坐标)但它捕获了窗口后面的所有内容,而不是窗口.

My problem comes when I use more than one screen or a programm like Teamviewer. The problem is, my programm captures the right area (good coordinates on the screen whenever which screen) but it captures all what's behind the window but not the window.

有人知道我做错了什么还是我错过了一个细节?

Does somebody knows what am I doing wrong or if I missed a detail ?

这是我目前使用的代码:

Here is the code I'm currently using :

// creates an rectangle of the size of the window
        Rectangle bounds = new Rectangle(
            (int)System.Windows.Application.Current.MainWindow.Left+10, 
            (int)System.Windows.Application.Current.MainWindow.Top+10, 
            (int)System.Windows.Application.Current.MainWindow.Width-20, 
            (int)System.Windows.Application.Current.MainWindow.Height-20);

        // creates a bitmap with a screenshot of the size of the window
        Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
        Graphics g = Graphics.FromImage(bitmap);
        g.CopyFromScreen(new System.Drawing.Point(bounds.Left, bounds.Top), new System.Drawing.Point(0,0), bounds.Size);

在此先感谢您提供任何帮助或示例.

Thanks in advance for any help or examples.

推荐答案

我不太明白你的意思.我认为您需要做的是捕获活动窗口.

I don't understand exactly you. I think you need to do is capture the active window.

Rectangle bounds = Screen.GetBounds(Point.Empty);
using(Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
    using(Graphics g = Graphics.FromImage(bitmap))
    {
         g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
    }
    bitmap.Save("test.jpg", ImageFormat.Jpeg);
}

//for capturing current window use

 Rectangle bounds = this.Bounds;
 using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
 {
    using (Graphics g = Graphics.FromImage(bitmap))
    {
        g.CopyFromScreen(new Point(bounds.Left,bounds.Top), Point.Empty, bounds.Size);
    }
    bitmap.Save("C://test.jpg", ImageFormat.Jpeg);
 }

来源:捕获活动窗口的屏幕截图?

这篇关于c#活动窗口的屏幕截图但不捕获窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆