屏幕截图我的表单的活动区域,没有任务栏和表单外部的边缘

问题描述 投票:0回答:1

我正在尝试捕获 Windows 窗体应用程序的屏幕截图,但是当我捕获屏幕截图时,任务栏位于应用程序的顶部和边缘。我只想捕获应用程序内部的屏幕截图,不包括应用程序和任务栏外部的外边缘。这是我用来创建应用程序屏幕截图的代码。

    private void captureScreenshot()
    {
        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:\\Users\\Brandon\\Downloads\\Test.jpg", ImageFormat.Jpeg);
        }
    }

我假设应用程序的边界并不完全是我想要的屏幕截图以获得我想要的效果,因为它捕获任务栏并稍微超出我的应用程序的边缘。

这是我得到的屏幕截图:

这是我要截图的区域的图片(以黄色框出):

c# winforms visual-studio screenshot
1个回答
0
投票

我也在尝试同样的事情 尝试这个代码,但我的问题是

1-获取实际的屏幕比例和布局,以获取实际的表单大小和位置,例如我的屏幕尺寸 1920 * 1080,比例为 125% 当我试图得到 var screenWidth = SystemParameters.PrimaryScreenWidth; var screenHeight = SystemParameters.PrimaryScreenHeight; 我得到了结果 屏幕宽度 = 1920/54 = 1536 ,屏幕高度 = 1080/54 = 864 所以到现在为止你需要像我一样手动调整

int captureX = pictureBox2.Location.X / 4 * 5 + this.Location.X / 4 * 5; int captureY = pictureBox2.Location.Y / 4 * 5 + this.Location.Y / 4 * 5;

2- CopyFromScreen 复制包括表单在内的整个屏幕,因此为了捕获表单后面的区域,您必须添加 opacity = 0 ;那么不透明度=1;
或使用 this.Visible = false;和 this.Visible = true;移动表单时会导致闪烁

 private void CaptureResizeAndSetBackgroundpictureBox()
    {
        //// Define the capture area (e.g., the entire screen or a specific region)
        //int captureX = pictureBox2.Location.X + pictureBox2.Location.X / 4;
        //int captureY = pictureBox2.Location.Y + pictureBox2.Location.Y / 4;
        //int captureWidth = pictureBox2.Width + pictureBox2.Width / 4;  // Width of the form
        //int captureHeight = pictureBox2.Height + pictureBox2.Height / 4; // Height of the form
        this.Visible = false;

        int captureX = pictureBox2.Location.X / 4 * 5 + this.Location.X / 4 * 5;
        int captureY = pictureBox2.Location.Y / 4 * 5 + this.Location.Y / 4 * 5;
        int captureWidth = pictureBox2.Width + pictureBox2.Width / 4;  // Width of the form
        int captureHeight = pictureBox2.Height + pictureBox2.Height / 4; // Height of the form

        // Capture the screen within the defined area
        // Bitmap capturedImage = new Bitmap(captureWidth, captureHeight);
        Bitmap capturedImage = new Bitmap(captureWidth, captureHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        using (Graphics captureGraphics = Graphics.FromImage(capturedImage))
        {
            captureGraphics.CopyFromScreen(captureX, captureY, 0, 0, new Size(captureWidth, captureHeight));
        }

        // Resize the captured image if needed (optional)
        int newWidth = pictureBox2.Width; // Set your desired new width
        int newHeight = pictureBox2.Height; // Set your desired new height
        if (capturedImage.Width != newWidth || capturedImage.Height != newHeight)
        {
            capturedImage = new Bitmap(capturedImage, newWidth, newHeight);
        }
        // capturedImage.MakeTransparent(capturedImage.GetPixel(this.Location.X, this.Location.Y));
        // Set the resized image as the background of the form
        pictureBox2.Image = capturedImage;
        this.Visible = true;
    }
© www.soinside.com 2019 - 2024. All rights reserved.