我的第一個SO問題:-)我希望通過在Windows計算機上調用User32.dll和GDI32.dll(項目需求)來從Golang獲取屏幕截圖。將GDI32.dll位圖保存到Golang中的磁盤
我有一個包含屏幕截圖像素的位圖句柄。但是,我不知道如何訪問其數據或如何將其保存到磁盤。任何人都知道如何將GDI位圖映射到Golang []字節,然後保存爲JPG或PNG格式?
package main
import "syscall"
var (
user32 = syscall.NewLazyDLL("user32.dll")
procGetClientRect = user32.NewProc("GetClientRect")
// etc...
gdi32 = syscall.NewLazyDLL("gdi32.dll")
procCreateDC = gdi32.NewProc("CreateDC")
SRCCOPY uint = 13369376
//etc...
)
//
// omitted for brevity
//
func TakeDesktopScreenshotViaWinAPI() {
// these are all calls to user32.dll or gdi32.dll
hDesktop := GetDesktopWindow()
desktopRect := GetClientRect(hDesktop)
width := int(desktopRect.Right - desktopRect.Left)
height := int(desktopRect.Bottom - desktopRect.Top)
// create device contexts
srcDC := GetDC(hDesktop)
targetDC := CreateCompatibleDC(srcDC)
// create bitmap to copy to
hBitmap := CreateCompatibleBitmap(targetDC, width, height)
// select the bitmap into target DC
hOldSelection := SelectObject(targetDC, HGDIOBJ(hBitmap))
//bit block transfer from src to target
BitBlt(targetDC, 0, 0, width, height, srcDC, 0, 0, SRCCOPY)
// how to save the the data in
// *hBitmap ???
// restore selection
SelectObject(targetDC, hOldSelection)
// clean up
DeleteDC(HDC(targetDC))
ReleaseDC(hDesktop, srcDC)
DeleteObject(HGDIOBJ(hBitmap))
}
封裝'圖像\ jpeg'提供了一個解碼器/編碼器,其從讀出io.Reader。我認爲,如果'GDIbitmap'不符合Reader接口,它不會很難擴展它。 – miltonb
謝謝@miltonb,試一試。將回復我的發現。 – Gautam
結束了使用vova庫,而不是乾淨地返回golang的矩形結構。 – Gautam