使用 Ruby 的桌面屏幕截图

时间:2021-05-07 15:11:39

标签: ruby screenshot desktop

我目前正在使用一个 Ruby 脚本来捕获需要保存的桌面屏幕。但是,图像(base64 字符串)的大小非常大。这个需要压缩。请求帮助压缩此图像并获取压缩的 base64 字符串。

参考:https://github.com/jarmo/win32screenshot/blob/master/lib/win32/screenshot/bitmap_maker.rb

我当前的 Ruby 脚本粘贴在下面。

def self.get_screen_image
  handle = Windows::Win.desktop_window
  hScreenDC = Windows::Win.window_dc(handle)
  screen_size = `wmic path Win32_VideoController get CurrentHorizontalResolution,CurrentVerticalResolution`
  width = screen_size.scan(/\d+/)[0].to_i
  height = screen_size.scan(/\d+/)[1].to_i
  hmemDC = Windows::Win.create_compatible_dc(hScreenDC)
  hmemBM = Windows::Win.create_compatible_bitmap(hScreenDC, width, height)
  Windows::Win.select_object(hmemDC, hmemBM)
  Windows::Win.bit_blt(hmemDC, 0, 0, width, height, hScreenDC, 0, 0, Windows::Win::SRCCOPY)
  begin
    bitmap_size = width * height * 3 + width % 4 * height
    bitmap_data = FFI::MemoryPointer.new(bitmap_size)
    bitmap_info = [40, width, height, 1, 24, 0, 0, 0, 0, 0, 0].pack('L3S2L6')
    Windows::Win.di_bits(hmemDC, hmemBM, 0, height, bitmap_data, bitmap_info, Windows::Win::DIB_RGB_COLORS)
    bitmap_file_header = [
      19778,
      bitmap_size + 40 + 14,
      0,
      0,
      54
    ].pack('SLSSL')
    Base64.encode64(bitmap_file_header + bitmap_info + bitmap_data.read_string(bitmap_size))
  rescue StandardError => e
    ''
  ensure
    bitmap_data.free
    Windows::Win.delete_object(hmemBM)
    Windows::Win.delete_dc(hmemDC)
    Windows::Win.release_dc(0, hScreenDC)
  end
end

我希望在 Ruby 中拥有与以下 .Net 代码等效的代码(理想情况下没有外部 gem 依赖项)

Rectangle rectangle = Screen.PrimaryScreen.Bounds;
using (Bitmap bitmap = new Bitmap(rectangle.Width, rectangle.Height))
using (Graphics g = Graphics.FromImage(bitmap))
using (MemoryStream stream = new MemoryStream())
{
    g.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty, rectangle.Size);
    bitmap.Save(stream, ImageFormat.Jpeg);
    return Convert.ToBase64String(stream.ToArray());
}

0 个答案:

没有答案