2017-08-09 8 views
1

J'ai créé un client qui envoie une capture d'écran sur TCP à un serveur. L'image est envoyée avec succès. Cependant, à la réception, l'image est à l'envers (je sais que cela est défini par la hauteur négative dans l'une des propriétés de la structure). J'ai cherché sur Internet sur les moyens de le faire, mais je ne pouvais pas trouver un moyen approprié pour y parvenir. Donc, s'il y a une meilleure façon de le faire, s'il vous plaît me corriger.L'image envoyée via TCP est retournée

Voici comment j'envoyer la capture d'écran (client):

HDC ScreenDC = GetDC(0); 
HDC hMemory = CreateCompatibleDC(ScreenDC); 
POINT ScreenSize = { GetDeviceCaps(ScreenDC, HORZRES),GetDeviceCaps(ScreenDC, VERTRES)}; 

// fill the hbitmap wtih the screenshot 
HBITMAP hBitmap = CreateCompatibleBitmap(ScreenDC, ScreenSize.x, ScreenSize.y); 
HGDIOBJ hOldBitmap = SelectObject(hMemory, hBitmap); 
BITMAPINFO bmpInfo = { 0 }; 

bmpInfo.bmiHeader.biSize = sizeof(bmpInfo.bmiHeader); 
GetDIBits(ScreenDC , hBitmap, 0, 0, NULL, &bmpInfo, DIB_RGB_COLORS); // fill bmpInfo with info about the hBitmap 
char * dataBuffer = new char[bmpInfo.bmiHeader.biSizeImage]; 

// adjust the header for our needs 
bmpInfo.bmiHeader.biBitCount = 32; 
bmpInfo.bmiHeader.biCompression = BI_RGB; // no compression -> easier to use 
bmpInfo.bmiHeader.biHeight = abs(bmpInfo.bmiHeader.biHeight); 
BitBlt(hMemory, 0, 0, ScreenSize.x, ScreenSize.y, ScreenDC, 0, 0, SRCCOPY); // take a screenshot 
GetDIBits(ScreenDC, hBitmap, 0, bmpInfo.bmiHeader.biHeight, dataBuffer, &bmpInfo, DIB_RGB_COLORS); // fill dataBuffer with raw image data 

// send first the bmpInfo.bmiHeader struct 
// send raw data : dataBuffer 
//..... 

Je suis sûr que le message est envoyé et reçu correctement comme je l'ai mis en place un protocole de séparation des packets.This est pas le problème.

C'est le récepteur (serveur):

// data is the entire sent : structure + dataBuffer 

HDC hDc = GetDC(windowHwnd); 
HDC tCpyHdc = CreateCompatibleDC(hDc); 

BITMAPINFOHEADER bmpHeader = *(BITMAPINFOHEADER*)data; 
BITMAP bmp; 
bmp.bmType = 0; 
bmp.bmWidth = bmpHeader.biWidth; 
bmp.bmHeight = abs(bmpHeader.biHeight); 
bmp.bmPlanes = bmpHeader.biPlanes; 
bmp.bmWidthBytes = bmpHeader.biWidth * 4; 
bmp.bmBitsPixel = bmpHeader.biBitCount; 
bmp.bmBits = (char*)(data + sizeof(BITMAPINFOHEADER)); 

HBITMAP hB = CreateBitmapIndirect(&bmp); 

HBITMAP oldBmp = (HBITMAP)SelectObject(tCpyHdc, hB); 
StretchBlt(hDc, 0, 0, width - 20, height - 40, tCpyHdc, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY); 

L'image est à l'envers .Comment puis-je résoudre ce problème?

+0

vous utilisez un bitmap indépendante du dispositif du côté émetteur (par l'intermédiaire de 'GetDIBits') mais un dépendant du dispositif sur le côté de réception (par l'intermédiaire de' CreateBi tmapIndirect'). Je suggère que vous utilisiez un DIB du côté réception, par exemple. via 'CreateDIBSection' –

+0

Je l'ai testé et je réponds si ça marche ou pas. Merci –

+0

Vous voulez dire CreateDIBitmap peut-être? parce que CreateDIBSection comme je peux le voir ne retourne pas un HBITMAP d'un tableau. Je crois, basé sur le HDC, il donne un tableau et un HBITMAP. Ce n'est pas ce que je veux sur le serveur. –

Répondre

3

Merci à @Igor Tandetnik J'ai résolu ce problème .C'est le code final:

Sur le client i envoie BITMAPINFO au lieu de BITMAPINFOHEADER:

//... 
// send first the bmpInfo struct 
// send raw data : dataBuffer 
//..... 

côté serveur (j'ai utilisé CreateDIBSection au lieu de CreateBitmapIndirect):

// data is the entire sent : structure + dataBuffer 

HDC hDc = GetDC(windowHwnd); 
HDC tCpyHdc = CreateCompatibleDC(hDc); 

BITMAPINFO bmpInfo = *(BITMAPINFO*)data; 
BITMAP bmp; 
bmp.bmType = 0; 
bmp.bmWidth = bmpInfo.bmiHeader.biWidth; 
bmp.bmHeight = -abs(bmpInfo.bmiHeader.biHeight); 
bmp.bmPlanes = bmpInfo.bmiHeader.biPlanes; 
bmp.bmWidthBytes = bmpInfo.bmiHeader.biWidth * 4; 
bmp.bmBitsPixel = bmpInfo.bmiHeader.biBitCount; 
bmp.bmBits = (char*)(data + sizeof(BITMAPINFO)); 

char* dibarr = NULL; 
HBITMAP hB = CreateDIBSection(windowData.tCpyHdc, &bmpInfo, DIB_RGB_COLORS, (void**)&dibarr, 0, 0); 
memcpy((void*)dibarr, (void*)bmp.bmBits, inf.packetsz - sizeof(BITMAPINFO)); 

HBITMAP oldBmp = (HBITMAP)SelectObject(tCpyHdc, hB); 
StretchBlt(hDc, 0, 0, width - 20, height - 40, tCpyHdc, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);