Visual Studio 我的表单的屏幕截图活动区域,没有边框和表单外部的边缘

o3imoua4  于 2023-10-23  发布在  其他
关注(0)|答案(1)|浏览(104)

我试图捕捉一个窗口窗体应用程序的屏幕截图,但当我捕捉屏幕截图时,我在应用程序的顶部和边缘得到了一个窗口。我只是想捕捉我的应用程序的内部部分的截图,不包括我的应用程序和外部边缘的外部。下面是我用来创建应用程序屏幕截图的代码。

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);
        }
    }

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

下面是我想截图的区域的图片(黄色轮廓):

eh57zj3b

eh57zj3b1#

我也在尝试同样的代码,但问题是
1-获取实际的屏幕比例和布局,以获取实际的窗体大小和位置,例如,当我尝试获取var screenWidth = SystemParameters. PrimaryScreenWidth时,我的屏幕大小为1920 * 1080,比例为125%; var screenHeight = SystemParameters. PrimaryScreenHeight;我得到的结果screenWidth = 1920/5 * 4 = 1536,screenHeight = 1080/5 * 4 = 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;那么opacity = 1;
或者使用这个。Visible = false;而这个. Visible = true;这会在移动窗体时导致 Flink

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;
    }

相关问题