You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用CopyFromScreen截图后排除中心区域并保存剩余部分?

解决方案

你可以通过两种方式实现排除中心区域的截图:

方法一:截取全屏后覆盖中心区域

基于你现有的代码,先截取完整屏幕,然后将中心1280x720的区域填充为指定颜色(比如黑色),代码修改如下:

private void CaptureImage(string _path)
{
    // FHD参数
    int _refX = 0;
    int _refY = 0;
    int _imgW = 1920;
    int _imgH = 1080;

    // 计算中心排除区域的坐标
    int centerExcludeX = (_imgW - 1280) / 2;
    int centerExcludeY = (_imgH - 720) / 2;
    Rectangle excludeRect = new Rectangle(centerExcludeX, centerExcludeY, 1280, 720);

    if (!string.IsNullOrEmpty(_path))
    {
        using (Bitmap bitmap = new Bitmap(_imgW, _imgH))
        {
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                // 截取全屏
                g.CopyFromScreen(_refX, _refY, 0, 0, bitmap.Size);
                // 用黑色覆盖中心需要排除的区域
                g.FillRectangle(Brushes.Black, excludeRect);
            }
            bitmap.Save(_path, ImageFormat.Png);
        }
    }
}

如果需要中心区域为透明,需创建支持Alpha通道的Bitmap并填充透明色:

private void CaptureImageWithTransparentCenter(string _path)
{
    int _refX = 0;
    int _refY = 0;
    int _imgW = 1920;
    int _imgH = 1080;

    int centerExcludeX = (_imgW - 1280) / 2;
    int centerExcludeY = (_imgH - 720) / 2;
    Rectangle excludeRect = new Rectangle(centerExcludeX, centerExcludeY, 1280, 720);

    if (!string.IsNullOrEmpty(_path))
    {
        // 创建带Alpha通道的Bitmap
        using (Bitmap bitmap = new Bitmap(_imgW, _imgH, PixelFormat.Format32bppArgb))
        {
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.CopyFromScreen(_refX, _refY, 0, 0, bitmap.Size);
                // 设置混合模式为覆盖,填充透明色
                g.CompositingMode = CompositingMode.SourceCopy;
                g.FillRectangle(Brushes.Transparent, excludeRect);
            }
            bitmap.Save(_path, ImageFormat.Png);
        }
    }
}

方法二:分区域截取并拼接

直接截取屏幕的四个外围区域,拼接成最终图片,避免截取中心不需要的区域:

private void CaptureExcludeCenter(string _path)
{
    int screenWidth = 1920;
    int screenHeight = 1080;
    int centerWidth = 1280;
    int centerHeight = 720;

    // 计算各区域参数
    int topHeight = (screenHeight - centerHeight) / 2;
    int sideWidth = (screenWidth - centerWidth) / 2;
    int bottomY = topHeight + centerHeight;
    int rightX = sideWidth + centerWidth;

    if (!string.IsNullOrEmpty(_path))
    {
        using (Bitmap bitmap = new Bitmap(screenWidth, screenHeight))
        {
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                // 截取顶部区域
                g.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, topHeight));
                // 截取底部区域
                g.CopyFromScreen(0, bottomY, 0, bottomY, new Size(screenWidth, topHeight));
                // 截取左侧中间区域
                g.CopyFromScreen(0, topHeight, 0, topHeight, new Size(sideWidth, centerHeight));
                // 截取右侧中间区域
                g.CopyFromScreen(rightX, topHeight, rightX, topHeight, new Size(sideWidth, centerHeight));
            }
            bitmap.Save(_path, ImageFormat.Png);
        }
    }
}

说明

  • 方法一实现简单,基于现有代码修改即可,适合快速需求;
  • 方法二更高效,只截取需要的区域,适合对性能有要求的场景;
  • 需确保引用必要命名空间:using System.Drawing;、using System.Drawing.Imaging;、using System.IO;

内容的提问来源于stack exchange,提问作者orpheus12

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 17:15:42