zoukankan      html  css  js  c++  java
  • C#截图操作(几种截图方法)

    公共函数
    获取屏幕截图
    private Bitmap GetScreenCapture()
    {
    Rectangle tScreenRect = new Rectangle(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
    Bitmap tSrcBmp = new Bitmap(tScreenRect.Width, tScreenRect.Height); // 用于屏幕原始图片保存
    Graphics gp = Graphics.FromImage(tSrcBmp);
    gp.CopyFromScreen(0, 0, 0, 0, tScreenRect.Size);
    gp.DrawImage(tSrcBmp, 0,0,tScreenRect,GraphicsUnit.Pixel);
    return tSrcBmp;
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    图片灰度化
    public static Bitmap ImgRgbToGray(Bitmap bitmap)
    {
    Bitmap b = new Bitmap(bitmap);
    BitmapData bmData = b.LockBits( new Rectangle(0, 0, b.Width, b.Height),
    ImageLockMode.ReadWrite,
    PixelFormat.Format24bppRgb);
    int stride = bmData.Stride; // 扫描的宽度
    unsafe
    {
    byte* p = (byte*)bmData.Scan0.ToPointer(); // 获取图像首地址
    int nOffset = stride - b.Width * 3; // 实际宽度与系统宽度的距离
    byte red, green, blue;
    for (int y = 0; y < b.Height; ++y)
    {
    for (int x = 0; x < b.Width; ++x)
    {
    blue = p[0];
    green = p[1];
    red = p[2];

    p[0] = p[1] = p[2] = (byte)(.299 * red + .587 * green + .114 * blue); // 转换公式

    p += 3; // 跳过3个字节处理下个像素点
    }
    p += nOffset; // 加上间隔
    }
    }
    b.UnlockBits(bmData); // 解锁
    return b;
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    截图方法的具体实现
    假定是在坐标为(55,88)的地方截取长宽为(520,233)的矩形,截图时整个背景灰化,截图区域显示原图

    使用像素替换的方法
    该方法代码最简单,核心函数是 GetPixel 、SetPixel ,但是速度不敢恭维!!!特别慢!!!!!!!!

    private Bitmap PixelReplace()
    {
    Bitmap tSrcBtm = GetScreenCapture();
    Bitmap tGrayBtm = ImgRgbToGray();
    for (int i = 55; i < 55 + 520; i++)
    {
    for (int k = 88; k < 88 + 233; k++)
    {
    tGrayBtm.SetPixel(i,k, tSrcBtm.GetPixel(i,k));
    }
    }
    return tGrayBtm;
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    在原图上画图的方法
    该方法比较快,但是每次都需要重新绘制,改变了底图

    private Bitmap DrawOnPicture()
    {
    int tAbsWidth = 1;
    Bitmap tSrcBtm = GetScreenCapture();
    Bitmap tGrayBtm = ImgRgbToGray();
    Rectangle tRectArea = new Rectangle(55, 88, 520, 233);
    Rectangle tDrawArea = new Rectangle(55 + tAbsWidth, 88 + tAbsWidth, 520 - tAbsWidth, 233 - tAbsWidth);
    Graphics gp = Graphics.FromImage(tGrayBtm);
    gp.DrawRectangle(Pens.Red, tRectArea);
    gp.DrawImage(tSrcBtm, tDrawArea, tRectArea, GraphicsUnit.Pixel);

    return tGrayBtm;
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    使用蒙板的方法
    实际上也是在在图片上画图,只不过背景上直接画了一个灰色的rect铺满屏幕,而不是取的灰度图

    public void CaptureScreen()
    {
    // 获取屏幕
    Bitmap btp = GetScreenCapture();
    Image BackScreen = new Bitmap(btp);

    // 在 BackScreen 上绘制蒙板
    Graphics g = Graphics.FromImage(BackScreen);
    g.FillRectangle(new SolidBrush(Color.FromArgb(100, 0, 0, 0)), 0, 0, BackScreen.Width, BackScreen.Height);
    this.BackgroundImage = BackScreen;

    // 最大化
    this.WindowState = FormWindowState.Maximized;

    // 不在任务栏显示
    this.ShowInTaskbar = false;
    this.TopMost = true;

    // 无边框
    this.FormBorderStyle = FormBorderStyle.None;

    // 在坐标为(55,88)的地方截取长宽为(520,233)的矩形
    Rectangle rect = new Rectangle(55, 88, 520, 233);
    g.DrawImage(btp, rect, rect, GraphicsUnit.Pixel);

    this.Show();
    }
    ————————————————
    版权声明:本文为CSDN博主「卢然小子」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/weixin_36381867/article/details/79434143

  • 相关阅读:
    go——数组
    go——流程控制
    go——基本类型
    go——基本构成要素
    go——常量
    go——变量
    go——标准命令
    go——工程结构
    python 优雅的使用正则表达式 ~ 1
    python 安装操作 MySQL 数据库.
  • 原文地址:https://www.cnblogs.com/huangcong/p/12020804.html
Copyright © 2011-2022 走看看