[英]C++ BITMAP to C# Bitmap
I am trying to create a simple screenshare program, with a C++ server, and a C# client. 我正在尝试使用C ++服务器和C#客户端创建一个简单的屏幕共享程序。 I am currently trying to achieve this by sending the buffer you get from BitBlt
, and sending that over the net. 我目前正在尝试通过发送从BitBlt
获得的缓冲区并通过网络发送它来实现此目的。 This all seems to go alright, but when I try to read the buffer in my C# client, the image looks messed up. 这一切似乎都没问题,但是当我尝试在C#客户端中读取缓冲区时,图像看起来很乱。 An example: 一个例子:
The code I'm using to get the buffer on the C++ end (Found this code somewhere): 我用来获取C ++端缓冲区的代码(在某处找到此代码):
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: 代码我用来在C#端显示图像:
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) 屏幕宽度:1366(客户端和服务器都有)
Screen Height: 768 (Also both the same, in client and server) 屏幕高度:768(在客户端和服务器中也是相同的)
For the buffer size, I am simply just using width * height * 3, in this case its 3147264 对于缓冲区大小,我只是使用width * height * 3,在本例中为3147264
In your C++-Code you have: bmi.biBitCount = 24;
在你的C ++ - 代码中你有: bmi.biBitCount = 24;
but in C# you use the default pixelformat for the bitmap. 但在C#中,您使用位图的默认pixelformat。 This is PixelFormat32bppArgb
. 这是PixelFormat32bppArgb
。 That means that it uses 32 bits per pixel. 这意味着它每像素使用32位。 If you use 如果你使用
Bitmap bitmap = new Bitmap(clientWidth, clientHeight, PixelFormat.Format24bppRgb);
This might fix the problem. 这可能会解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.