我正在尝试编写图像上传客户端。我使用这段代码来抓取屏幕截图并将其写入变量 hBitmap。然后我想使用这个下的代码上传它,但我不知道如何转换或重新格式化图像。我不想将图像写入文件然后读出文件,这很容易;)
// get the device context of the screen
HDC hScreenDC = CreateDC("DISPLAY", NULL, NULL, NULL);
// and a device context to put it in
HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
int x = GetDeviceCaps(hScreenDC, HORZRES);
int y = GetDeviceCaps(hScreenDC, VERTRES);
// maybe worth checking these are positive values
HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDc, x, y);
// get a new bitmap
HBITMAP hOldBitmap = SelectObject(hMemoryDC, hBitmap);
BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, 0, 0, SRCCOPY);
hBitmap = SelectObject(hMemoryDC, hOldBitmap);
// clean up
DeleteDC(hMemoryDC);
DeleteDC(hScreenDC);
这是我的上传代码:
void HTTP_UPLOAD_REQUEST(char * Server,int Port,char * uploadscript,char* boundary,char*filetoup,char* data)
{
//Create Socket for sending data
WSADATA wsaData;
WSAStartup( MAKEWORD( 1,1 ), &wsaData );
SOCKET Socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
LPHOSTENT hostEntry;
hostEntry = gethostbyname( Server );//Get ip from Server by hostname
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
addr.sin_port = htons( Port ); //Set Port
connect( Socket, (LPSOCKADDR) &addr, sizeof(struct sockaddr) );
//Socket created and connected to Server!
//Create HTTP POST Request
//construct POST Header
char header[512]="";
sprintf( header, "POST %s HTTP/1.0\r\nContent-Type: multipart/form-data; boundary=%s\r\nContent-Length: %u\r\n\r\n",uploadscript, boundary,2*strlen(data));
//construct Body(data part) of HTTP POST
char* body=new char[strlen(data)+4000];
sprintf( body, "--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n\r\n %s\r\n--%s\r\n",boundary,"data",filetoup,data,boundary);
//Put Header and Body together into Request
char * Request=new char[strlen(header)+strlen(body)+strlen(data)];
sprintf( Request, "%s%s", header,body );
//int bytestosend = strlen(Request);
int bytessend =send( Socket, Request, strlen(Request), 0 );
closesocket(Socket);//cleanup -!
}