通过TCP发送的图像被翻转

问题描述:

我创建了一个客户端,它通过TCP向服务器发送屏幕截图。图像发送成功。然而,在接收时,图像是颠倒的(我知道这是由结构的一个属性中的负高度设置的)。 我在互联网上搜索的方式,但我找不到一个正确的方式来实现这一点。所以,如果有更好的方式做到这一点,请纠正我。通过TCP发送的图像被翻转

这是我送的截图(客户端):

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 
//..... 

我相信正在发送的消息,因为我已经实现了一个协议,用于分离packets.This正确接收是没有问题的。

这是接收器(服务器):

// 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); 

的图像是上下颠倒我。如何解决这个问题?

+0

在发送端使用设备无关位图(通过'GetDIBits'),但在接收端使用与设备相关的位图(通过'CreateBi tmapIndirect')。我建议你也在接收端使用DIB,例如通过'CreateDIBSection' –

+0

我会测试它并回复它是否工作。谢谢 –

+0

你的意思是CreateDIBitmap也许?因为我可以看到CreateDIBSection不会从数组中返回一个HBITMAP。我相信,基于HDC,它提供了一个阵列和一个HBITMAP。这不是我想要的服务器上。 –

感谢@Igor Tandetnik我解决了这个问题。这是最后的代码:

在客户端我送BITMAPINFO而不是BITMAPINFOHEADER:

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

服务器端(我用CreateDIBSection代替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);