C# Windows登录界面进行截图,控制鼠标键盘等操作实现(三)

这篇具有很好参考价值的文章主要介绍了C# Windows登录界面进行截图,控制鼠标键盘等操作实现(三)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

在Windows登录界面进行截图,目前尝试到的以下的截图方法可以截取到图,其它的方式要么卡住,要么截出来是黑屏的,直接上代码:
/// <summary>使用Graphics方式捕获桌面截图(效率低于DesktopCapture,主要用户登录界面截图)</summary>
  internal class GraphicCapture
  {
    private const int Desktopvertres = 117;
    private const int Desktophorzres = 118;
    /// <summary>当前最新一帧</summary>
    private Bitmap _currentFrame;
    private Rectangle _currentScreenBounds;
    private Graphics _graphic;
    private readonly object _screenLock = new object();
    private Bitmap _originBitmap;private bool _isManualCaptureStop;
    private PixelFormat _pixelFormat = PixelFormat.Bgra32;

    private Size DesktopSize
    {
      get
      {
        IntPtr dc = GraphicCapture.GetDC(IntPtr.Zero);
        int deviceCaps1 = GraphicCapture.GetDeviceCaps(dc, 117);
        int deviceCaps2 = GraphicCapture.GetDeviceCaps(dc, 118);
        GraphicCapture.ReleaseDC(IntPtr.Zero, dc);
        return new Size(deviceCaps2, deviceCaps1);
      }
    }

    [DllImport("user32.dll")]
    private static extern IntPtr GetDC(IntPtr ptr);

    [DllImport("gdi32.dll")]
    private static extern int GetDeviceCaps(IntPtr hdc, int nIndex);

    [DllImport("User32.dll")]
    private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

    /// <summary>构造函数</summary>
    public GraphicCapture()
    {
      this._currentScreenBounds = this.GetScreenRectangle(Screen.PrimaryScreen);
      this._originBitmap = this.CreateBitmap();
      this._graphic = Graphics.FromImage((Image) this._originBitmap);
    }

    private Rectangle GetScreenRectangle(Screen screen)
    {
      Rectangle bounds = screen.Bounds;
      return new Rectangle(new Point(bounds.X, bounds.Y), this.DesktopSize);
    }

    /// <summary>Bitmap转byte[]</summary>
    /// <param name="bitmap"></param>
    /// <returns></returns>
    private byte[] BitmapToByteArray(Bitmap bitmap)
    {
      System.Drawing.Imaging.PixelFormat format = this.PixelFormatConverter(this.PixelFormat);
      Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
      BitmapData bitmapdata = bitmap.LockBits(rect, ImageLockMode.ReadOnly, format);
      int length = Math.Abs(bitmapdata.Stride) * bitmap.Height;
      byte[] destination = new byte[length];
      Marshal.Copy(bitmapdata.Scan0, destination, 0, length);
      bitmap.UnlockBits(bitmapdata);
      return destination;
    }

    private Bitmap CreateBitmap()
    {
      System.Drawing.Imaging.PixelFormat pixelFormat = this.PixelFormatConverter(this.PixelFormat);
      Size desktopSize = this.DesktopSize;
      int width = desktopSize.Width;
      desktopSize = this.DesktopSize;
      int height = desktopSize.Height;
      int format = (int) pixelFormat;
      return new Bitmap(width, height, (System.Drawing.Imaging.PixelFormat) format);
    }

    private System.Drawing.Imaging.PixelFormat PixelFormatConverter(
      PixelFormat customPixelFormat)
    {
      if (true)
        ;
      System.Drawing.Imaging.PixelFormat pixelFormat;
      if (customPixelFormat != PixelFormat.Bgra32)
      {
        if (customPixelFormat != PixelFormat.Bgr24)
          throw new ArgumentOutOfRangeException("PixelFormat", (object) this.PixelFormat, (string) null);
        pixelFormat = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
      }
      else
        pixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
      if (true)
        ;
      return pixelFormat;
    }

    /// <summary>检索最新的桌面图像和关联的元数据</summary>
    private byte[] GetLatestFrameToByte() => this.BitmapToByteArray(this.GetLatestFrameToBitmap());

    /// <summary>检索最新的桌面图像</summary>
    /// <returns></returns>
    private Bitmap GetLatestFrameToBitmap()
    {
      lock (this._screenLock)
      {
        Graphics graphic = this._graphic;
        int x = this._currentScreenBounds.X;
        int y = this._currentScreenBounds.Y;
        Size desktopSize = this.DesktopSize;
        int width1 = desktopSize.Width;
        desktopSize = this.DesktopSize;
        int height1 = desktopSize.Height;
        Size blockRegionSize = new Size(width1, height1);
        graphic.CopyFromScreen(x, y, 0, 0, blockRegionSize);
        Size newSize;
        ref Size local = ref newSize;
        desktopSize = this.DesktopSize;
        int width2 = (int) ((double) desktopSize.Width * this.Scale);
        desktopSize = this.DesktopSize;
        int height2 = (int) ((double) desktopSize.Height * this.Scale);
        local = new Size(width2, height2);
        this._currentFrame = newSize.Width == this._originBitmap.Width && newSize.Height == this._originBitmap.Height ? this._originBitmap : new Bitmap((Image) this._originBitmap, newSize);
      }
      return this._currentFrame;
    }

    /// <summary>捕获图像像素格式,默认为BGRA32</summary>
    public PixelFormat PixelFormat
    {
      get => this._pixelFormat;
      set
      {
        this._pixelFormat = value;
        this._originBitmap?.Dispose();
        this._graphic?.Dispose();
        this._originBitmap = this.CreateBitmap();
        this._graphic = Graphics.FromImage((Image) this._originBitmap);
      }
    }

    /// <summary>捕获图像缩放大小,默认为1.0</summary>
    public double Scale { get; set; } = 1.0;

    /// <summary>新帧捕获事件</summary>
    public event EventHandler<CaptureFrame> FrameArrived;

    /// <summary>开始捕获</summary>
    public void StartCapture()
    {
      this._isManualCaptureStop = false;
    }

    /// <summary>停止捕获</summary>
    public void StopCapture()
    {
      this._cancellationTokenSource?.Cancel();
      this._isManualCaptureStop = true;
    }

    /// <summary>获取下一帧图像数据</summary>
    /// <param name="captureFrame"></param>
    /// <returns></returns>
    public bool TryGetNextFrame(out CaptureFrame captureFrame)
    {
      captureFrame = (CaptureFrame) null;
      if (this._isManualCaptureStop)
        return false;try
      {
        byte[] byteArray = this.BitmapToByteArray(this.GetLatestFrameToBitmap());
        Size size = new Size((int) ((double) this.DesktopSize.Width * this.Scale), (int) ((double) this.DesktopSize.Height * this.Scale));
        captureFrame = new CaptureFrame(size, this.PixelFormat, byteArray);
        return true;
      }
      catch (Exception ex)
      {
        return false;
      }
    }
  }

操作鼠标键盘等操作可以参考本人另一篇文章:使用C#制作可以录制自动化执行Windows操作脚本工具——类似于按键精灵 - log9527 - 博客园 (cnblogs.com)文章来源地址https://www.toymoban.com/news/detail-654875.html

到了这里,关于C# Windows登录界面进行截图,控制鼠标键盘等操作实现(三)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 键盘怎么控制鼠标?键盘控制鼠标实现点击和移动

    鼠标在电脑操作过程中非常重要,可是有时遇到尴尬的问题,就是摸鱼的时候觉得一直点击鼠标声音太大,或者舍友都睡觉了,你突然之间需要浏览一些网页,一直点击鼠标会产生很大的噪声,这时候该怎么办呢?如果你是笔记本,配合鼠标版可以解决。如果你正好有一个静

    2024年02月06日
    浏览(39)
  • 安装黑苹果进入语言选择界面,鼠标/键盘无法使用?

    是因为 EFI 文件中没有 USB 驱动,或你使用的 EFI 文件定制了 USB 端口,但和你的主板不匹配。可以放一个 USBInjectAll.kext 到 EFI/Clover/Kexts/other ( Clover 方式),或 EFI/OC/Kexts ( OpenCore 方式),并删除同目录下的 USBPorts.kext ,或删除 ACPI/Patched 下的 ssdt-uiac.aml ( OpenCore 在 ACPI 目录,也可能

    2024年02月12日
    浏览(29)
  • 工具-Snipaste与ScreenToGif 生产力工具,对截图进行勾画操作,并可将截图贴至电脑任意界面;快捷动态截图成gif

    进入官网,可根据系统进行下载 https://zh.snipaste.com/ 傻瓜式安装成功后,电脑的右下角有个小图标,有些是彩色,有些是黑灰色,影响不大 傻瓜式安装,默认的截图快捷键就很好用 摁下F1,进行截屏,用工具可进行勾画,直接ctrl CV可以复制粘贴使用 摁下F1,勾画完成后,点击

    2024年02月01日
    浏览(26)
  • JAVA 鼠标控制与键盘输入控制

    该类是JDK定义的电脑系统的抽象类,可以用来模拟实现鼠标点击与键盘输入等信息 简单实现一个自动抢票代码: InputEvent.BUTTON1_MASK 左键 (食指点击) InputEvent.BUTTON2_MASK 中键 (滚轮) InputEvent.BUTTON3_MASK 右键(中指点击) 得到的信息需要根据屏幕--显示设置--缩放与布局的百分

    2024年02月13日
    浏览(27)
  • C# 监测全局键盘和鼠标事件

    在 C# 中,我们可以编写代码来监测全局键盘和鼠标事件。通过捕捉这些事件,我们可以实现一些有趣的功能,比如记录按键操作、截获特定的快捷键,或者在鼠标点击时执行特定的操作。 下面是一个示例代码,演示了如何在 C# 中监测全局键盘和鼠标事件:

    2024年02月02日
    浏览(34)
  • C#实现键盘鼠标模拟器

    下面程序可指定一连串重复动作,按顺序执行   using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Text.RegularExpressions; using System.Windows.Forms; namespace Simulator {     public partial class Form1 : Form     {         [System.Runtime.InteropServices.DllImp

    2024年02月16日
    浏览(34)
  • C# 实现按键精灵 记录录制键盘鼠标

     是一个非常实用的键盘鼠标脚本录制工具,通过它你可以轻松地进行脚本录制,简单易用,不需要任何编程知识就能做出功能强大的脚本,只要你在电脑前用双手可以完成的动作,都可以替你完成。                下载软件 1.运行录制脚步时模拟过程 比按键精灵 更加流畅

    2024年02月11日
    浏览(30)
  • python编程控制键盘鼠标

    1.安装Pywin32 下载完成后直接运行。 2. 模拟按键      keybd_event(bVk, bScan, dwFlags, dwExtraInfo)       第一个参数:虚拟键码(键盘键码对照表见附录);       第二个参数:硬件扫描码,一般设置为0即可;       第三个参数:函数操作的一个标志位,如果值为KEYEVENTF_EXTENDEDKEY则

    2024年02月08日
    浏览(29)
  • python读取控制鼠标键盘

    目录 一,工具 二,鼠标 1,实时显示鼠标位置 2,控制移动鼠标 3,控制点击鼠标 三,键盘 1,单键输入 2,组合键输入 四,实用demo 1,多网页依次点击固定位置的按钮 2,收集多个网页的链接 pyautogui库 命令:pip3 install pyautogui==0.9.50 如果不指定版本,可能会在使用时报错:

    2024年04月10日
    浏览(30)
  • python 如何控制鼠标键盘

    你可以使用Python的第三方库pyautogui来控制鼠标和键盘。pyautogui库是一个跨平台的GUI自动化库,可以模拟鼠标和键盘操作,以及截屏、获取窗口句柄等功能。 下面是一些常用的鼠标和键盘控制示例: 鼠标移动到指定位置 鼠标点击

    2024年02月11日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包