【发布时间】:2013-11-19 22:28:46
【问题描述】:
我正在尝试以这种方式在我的控制台 win32 应用程序中制作屏幕截图:
char* getScreenshot(){
HDC hScreenDC = CreateDC("DISPLAY", NULL, NULL, NULL);
HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
int x = GetDeviceCaps(hScreenDC, HORZRES);
int y = GetDeviceCaps(hScreenDC, VERTRES);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, x, y);
size_t width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
size_t height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemoryDC, hBitmap);
BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, 0, 0, SRCCOPY);
hBitmap = (HBITMAP)SelectObject(hMemoryDC, hOldBitmap);
char* buffer = new char[x*y]();
GetBitmapBits(hBitmap,x*y,buffer);
DeleteDC(hMemoryDC);
DeleteDC(hScreenDC);
return buffer;
}
buffer 应该是包含 hbitmap 截图的字节数组。
然后我以这种方式通过套接字发送它:
char *buff = getScreenshot();
string a(buff);
send(hClientSocket, buff, a.length(), 0);
if (hClientSocket!=INVALID_SOCKET)
closesocket(hClientSocket);
我以这种方式从 php 中捕获 if:
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
set_time_limit(15);
$succ = socket_connect($sock, $ip, $port) or die("timeout");
$output = "";
while($resp = socket_read($sock, 1000)) {
$output .= $resp;
}
socket_close($sock);
$base64 = base64_encode($output);
echo "<img src='data:image/bmp;base64,$base64' />";
我的问题是我无法使用上面写的方法显示图像。
可能是什么问题? 我哪里错了?
谢谢!
编辑
根据@Peter R. Bloomfield 所说,我将函数更改为:
char* getScreenshot(int* length){
...
*length = x*y;
...
}
并发送到套接字:
int length;
char *buff = getScreenshot(&length);
send(hClientSocket, buff, length, 0);
但它继续避免显示...
【问题讨论】: