截图创建和上传

时间:2013-10-07 19:15:49

标签: c++ image image-processing dev-c++

我正在尝试编写一个Image Uploading客户端。我使用此代码来获取屏幕截图并将其写入变量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 -!

}

1 个答案:

答案 0 :(得分:0)

  1. 您现在拥有的是原始位图数据。有效位图(* .bmp)文件也必须包含BMP标头。关于它有一个很好的article

    也许那时你想将巨大的bmp文件转换为jpeg或png格式。例如,Free image能够执行此类内存转换。

  2. 如果您需要将数据上传到网络,请考虑使用一些可靠的网络库,如libcurl。您的实现有一些内存泄漏。

    我的建议也是尽可能使用std::stringstd::vector。它不仅为您提供无泄漏,而且还提供更好的代码:

    void HTTP_UPLOAD_REQUEST(std::string Server, unsigned short Port, std::string uploadscript, std::string boundary, std::string filetoup, std::string data)
    {
        ...
    
        std::stringstream out;
    
        out << "POST " << uploadscript << " HTTP/1.0\r\n";
        out << "Content-Type: multipart/form-data; boundary=" << boundary << "\r\n";
        out << "Content-Length: " << data.size();
    
        out << "--" << boundary << "\r\n";
        out << "Content-Disposition: form-data; name=\"data\"; filename=\""
            << filetoup << "\"\r\n\r\n";
    
        out << data << "\r\n--" << boundary << "\r\n";
    
        std::string toSend = out.str();
        int bytessend = send( Socket, toSend.c_str(), toSend.size(), 0 ); 
        ...
    }
    

    请注意,WSAStartup只能在程序中调用一次,而不是每次调用HTTP_UPLOAD_REQUEST