简体   繁体   English

如何使用WIN32和C ++通过套接字发送HBITMAP?

[英]How to send a HBITMAP over a socket using WIN32 and C++?

I have created an application where I am required to send the cropped part of an image over a Socket. 我创建了一个应用程序,需要在该应用程序上通过套接字发送图像的裁剪部分。 I have cropped the image using StretchBlt(), I am also able to display and save the cropped image to a bitmap file. 我已经使用StretchBlt()裁剪了图像,也能够显示并保存裁剪后的图像到位图文件。

Next, I want to send this cropepd image over a Socket connection. 接下来,我想通过套接字连接发送此ropepd图像。 The connection between socket is established, My problem is how to send/ receive this image over Socket since send() and recv() functions require a char *. 建立套接字之间的连接,我的问题是如何通过Socket发送/接收此图像,因为send()和recv()函数需要使用char *。

One idea, is if I select the HBITMAP to a memDC object than can I send the memDC directly to the remote socket ? 一个想法是,如果我选择HBITMAP到memDC对象,那么我可以直接将memDC发送到远程套接字吗?

Any easier way to achieve this task? 有什么简单的方法可以完成这项任务?

You can't pass an HBITMAP (which is a HANDLE, a reference pointer in memory) to another machine. 您不能将HBITMAP(这是HANDLE,即内存中的引用指针)传递给另一台计算机。 You can use the GetBitmapBits Function to transform the HBITMAP into a byte array and send this byte array using send(). 您可以使用GetBitmapBits函数将HBITMAP转换为字节数组,然后使用send()发送此字节数组。

On the other side, you'll have to create another compatible bitmap, and use SetBitmapBits Function after the recv(). 另一方面,您必须创建另一个兼容的位图,并在recv()之后使用SetBitmapBits函数

Generally sending the raw memory would be a bad idea - even if you guarantee that there's always a windows machine on the other end today, it's not very flexible for the future - what if you want to change to a different platform, or even start using .NET over plain win32? 通常,发送原始内存是个坏主意-即使您保证今天另一端总是有Windows计算机,但对于将来却不太灵活-如果要更改为其他平台甚至开始使用该怎么办? .NET通过纯Win32?

What I'd recommend doing is saving the cropped image out to a .bmp file, then sending the file over the socket like any other. 我建议做的是将裁剪后的图像保存为.bmp文件,然后像其他任何文件一样通过套接字发送该文件。 That way you have maximum flexibility for what you're doing. 这样,您就可以最大程度地灵活地进行自己的工作。

Unfortunately, if you're working in C++/win32 outputting a bitmap is a bit of a nightmare. 不幸的是,如果您在C ++ / win32中工作,则输出位图是一场噩梦。 Here's the code to output a bitmap that I knocked together based on a few sources - it uses some of our class members, but you should be able to figure out how to plug in the right values there. 这是用于输出位图的代码,我根据一些来源将它们组合在一起-它使用了我们的某些类成员,但是您应该能够弄清楚如何在此处插入正确的值。

http://pastebin.com/gLw7ykMU http://pastebin.com/gLw7ykMU

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM