[Win32utils-devel] Screen capture, save to file
Paul Rogers
paul.rogers at shaw.ca
Thu Apr 21 16:28:43 EDT 2005
Is there anything in the utils to capture a screen ( or maybe just a
window) and save to a file.
I have this, but I have no idea where it came from, but it gives a
corrupted image
Paul
# -*- ruby -*-
# screen capture --
# This script runs with a Ruby/DL which is included in ruby-1.7.
require 'dl/import'
module ScreenCapture
extend DL::Importable
dlload "kernel32.dll","user32.dll","gdi32.dll"
HORZRES = 8
VERTRES = 10
SRCCOPY = 0xCC0020
GMEM_FIXED = 0
GMEM_MOVEABLE = 0x0002
DIB_RGB_COLORS = 0
GHND = 0x40
GPTR = 0x42
typealias "HDC","unsigned long"
typealias "HBITMAP","unsigned long"
extern "HDC GetWindowDC(int)"
extern "HDC CreateCompatibleDC(HDC)"
extern "int GetDeviceCaps(HDC, int)"
extern "HBITMAP CreateCompatibleBitmap(HDC, int, int)"
extern "long SelectObject(HDC, HBITMAP)"
extern "long BitBlt(HDC, long, long, long, long, HDC, long, long,
long)"
extern "void* GlobalAlloc(long, long)"
extern "void* GlobalLock(void*)"
extern "long GetDIBits(HDC, HBITMAP, long, long, void*, void*, long)"
extern "long GlobalUnlock(void*)"
extern "long GlobalFree(void*)"
extern "long DeleteObject(unsigned long)"
extern "long DeleteDC(HDC)"
extern "long ReleaseDC(long, HDC)"
module_function
def screenCapture(filename = "tmp.bmp")
hScreenDC = getWindowDC(0)
hmemDC = createCompatibleDC(hScreenDC)
screenWidth = getDeviceCaps(hScreenDC, HORZRES)
screenHeight = getDeviceCaps(hScreenDC, VERTRES)
hmemBM = createCompatibleBitmap(hScreenDC, screenWidth,
screenHeight)
selectObject(hmemDC, hmemBM)
printf("width,height = #{screenWidth},#{screenHeight}\n")
r = bitBlt(hmemDC, 0, 0, screenWidth, screenHeight, hScreenDC, 0, 0,
SRCCOPY)
printf("bitBlt : #{r}\n")
hpxldata = globalAlloc(GMEM_FIXED, screenWidth * screenHeight * 3)
lpvpxldata = globalLock(hpxldata)
bmInfo = [
40, screenWidth, screenHeight, 1, 24,
0, 0, 0, 0, 0, 0, 0].pack('LLLSSLLLLLL').to_ptr
bmFileHeader = [
19778, (screenWidth * screenHeight * 3) + 40 + 14,
0, 0, 54].pack('ILIIL').to_ptr
r = getDIBits(hmemDC, hmemBM, 0, screenHeight,
lpvpxldata, bmInfo, DIB_RGB_COLORS)
printf("getDIBits : #{r}\n")
File::open(filename, "w") { |bitmap|
bitmap.print(bmFileHeader.to_s(14))
bitmap.print(bmInfo.to_s(40))
bitmap.print(lpvpxldata.to_s(screenWidth * screenHeight * 3))
}
globalUnlock(hpxldata)
globalFree(hpxldata)
deleteObject(hmemBM)
deleteDC(hmemDC)
releaseDC(0, hScreenDC)
end
end
ScreenCapture::screenCapture()
More information about the win32utils-devel
mailing list