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

Flutter开发中如何截取屏幕指定区域的截图

enter image description here

屏幕指定区域图像提取实现方案

你可以根据自己的开发环境选择对应实现方式:

Python 快速实现

依赖Pillow库,先执行pip install pillow安装,再运行以下代码:

from PIL import ImageGrab

# 替换为你的实际参数
left_x = 100    # 区域左上角x坐标
top_y = 100     # 区域左上角y坐标
width = 300     # 区域宽度
height = 200    # 区域高度

# bbox参数格式为(左上角x, 左上角y, 右下角x, 右下角y)
captured_img = ImageGrab.grab(bbox=(left_x, top_y, left_x + width, top_y + height))

# 保存截图到本地,也可直接对captured_img做后续处理
captured_img.save("target_region.png")

注意:macOS下需要给代码运行的终端/IDE开启屏幕录制权限,否则会返回黑屏内容。

Windows 桌面开发实现(C# 示例)

直接调用系统内置的CopyFromScreen接口即可:

using System.Drawing;

public Bitmap CaptureSpecifiedRegion(int x, int y, int width, int height)
{
    Bitmap regionBitmap = new Bitmap(width, height);
    using (Graphics graphics = Graphics.FromImage(regionBitmap))
    {
        graphics.CopyFromScreen(x, y, 0, 0, new Size(width, height));
    }
    return regionBitmap;
}

浏览器网页区域提取(JS 示例)

使用html2canvas库实现网页指定区域截取:

// 先安装html2canvas:npm install html2canvas
import html2canvas from 'html2canvas';

// 替换为你自己的坐标参数
const captureOption = {
  x: 100,
  y: 100,
  width: 300,
  height: 200
};

html2canvas(document.body, captureOption).then(canvas => {
  // 导出为图片,或直接插入页面展示
  const imgUrl = canvas.toDataURL('image/png');
});

内容的提问来源于stack exchange,提问作者Zain Ur Rehman

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 06:45:04