Class: Win32::Screenshot::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/win32/screenshot/image.rb

Overview

Holds the bitmap data and writes it to the disk

Constant Summary collapse

FORMATS =

Supported output formats

%w{bmp gif jpg png}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bitmap (readonly)

String

raw bitmap blob



6
7
8
# File 'lib/win32/screenshot/image.rb', line 6

def bitmap
  @bitmap
end

#height (readonly)

String

bitmap height



12
13
14
# File 'lib/win32/screenshot/image.rb', line 12

def height
  @height
end

#width (readonly)

String

bitmap width



9
10
11
# File 'lib/win32/screenshot/image.rb', line 9

def width
  @width
end

Instance Method Details

#write(file_path)

Writes image to the disk.

Parameters:

  • file_path (String)

    writes image to the specified path.

Raises:

  • (RuntimeError)

    when file_path already exists.

  • (RuntimeError)

    when file_path is not with the supported output FORMATS extension.



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/win32/screenshot/image.rb', line 28

def write(file_path)
  raise "File already exists: #{file_path}!" if File.exists? file_path
  ext = File.extname(file_path)[1..-1]
  raise "File '#{file_path}' has to have one of the following extensions: #{FORMATS.join(", ")}" unless ext && FORMATS.include?(ext.downcase)

  if ext.downcase == "bmp"
    File.open(file_path, "wb") {|io| io.write @bitmap}
  else
    image = ::MiniMagick::Image.read @bitmap
    image.format ext
    image.write file_path
  end
end