I am trying to create a simple screenshare program, with a C++ server, and a C# client. I am currently trying to achieve this by sending the buffer you get from BitBlt, and sending that over the net. This all seems to go alright, but when I try to read the buffer in my C# client, the image looks messed up. An example: 
The code I'm using to get the buffer on the C++ end (Found this code somewhere):
void ScreenCap()
{
HDC hdc = GetDC(NULL), hdcMem = CreateCompatibleDC (hdc);
HBITMAP hBitmap = CreateCompatibleBitmap(hdc, ScreenX, ScreenY);
BITMAPINFOHEADER bmi = {0};
bmi.biSize = sizeof(BITMAPINFOHEADER);
bmi.biPlanes = 1;
bmi.biBitCount = 24;
bmi.biWidth = ScreenX;
bmi.biHeight = -ScreenY;
bmi.biCompression = BI_RGB;
SelectObject(hdcMem, hBitmap);
BitBlt(hdcMem, 0, 0, ScreenX, ScreenY, hdc, 0, 0, SRCCOPY);
int res = GetDIBits(hdc, hBitmap, 0, ScreenY, ScreenData, (BITMAPINFO*)&bmi, DIB_RGB_COLORS);
DeleteObject(hBitmap);
DeleteDC(hdcMem);
ReleaseDC(NULL, hdc);
}
Code I'm using to display the image on the C# end:
char[] buffer = packet.getData();
Bitmap bitmap = new Bitmap(clientWidth, clientHeight);
BitmapData bData = bitmap.LockBits(new Rectangle(0, 0, clientWidth, clientHeight), ImageLockMode.ReadWrite, bitmap.PixelFormat);
Marshal.Copy(Helper.toByteArray(buffer), 0, bData.Scan0, buffer.Length);
bitmap.UnlockBits(bData);
pictureBox1.Invoke(new Action(() =>
{
pictureBox1.Image = bitmap;
}));
I honestly have no idea whats going on.
EDIT
Some additional information:
Screen Width: 1366 (Both in client and the server)
Screen Height: 768 (Also both the same, in client and server)
For the buffer size, I am simply just using width * height * 3, in this case its 3147264
buffer.Length,clientWidthandclientHeight? And also there might be some confusion: sizeof(char) in C++ is guaranteed to be 1, in .NEt its usually 2.Bitmap bitmap = new Bitmap(clientWidth, clientHeight, PixelFormat.Format24bppRgb);