What you have now is a raw bitmap data. Valid bitmap (*.bmp) file must contain BMP header also. There is a good article about it.
Maybe then you'd like to convert the huge bmp file to jpeg or png format. Free image, for example, is able to perform such in-memory conversion.
If you need to upload data to the web consider using some reliable network library like libcurl. Your implementation has a few memory leaks.
My recommendation also is to use
std::string
orstd::vector
wherever is possible. It gives you not only leak-free but also nicer code: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 ); ... }
Note that
WSAStartup
must be called only once in a program, not each call toHTTP_UPLOAD_REQUEST
Screenshot Creation and Uploading
-
30-06-2022 - |
Question
I'm trying to write an Image Uploading client. I use this code to grab the screenshot and write it into the variable hBitmap. Then I want to Upload it using the code under this one, but I don't know how to convert or maybe reformat the image. I don't want to write the Image into an File and then readout the file, that would be to easy ;)
// 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);
This is My Uploading code:
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 -!
}
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow