如何使用 WIN32 和 C++ 通过套接字发送 HBITMAP?
我创建了一个应用程序,需要通过套接字发送图像的裁剪部分。我已经使用 StretchBlt() 裁剪了图像,我还可以显示裁剪后的图像并将其保存到位图文件中。
接下来,我想通过套接字连接发送这个croepd 图像。套接字之间的连接已建立,我的问题是如何通过套接字发送/接收该图像,因为send()和recv()函数需要一个char *。
一种想法是,如果我选择 HBITMAP 到 memDC 对象,那么我可以将 memDC 直接发送到远程套接字吗?
有更简单的方法来完成这项任务吗?
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.
Next, I want to send this cropepd image over a Socket connection. 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 *.
One idea, is if I select the HBITMAP to a memDC object than can I send the memDC directly to the remote socket ?
Any easier way to achieve this task?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法将 HBITMAP(它是 HANDLE,内存中的引用指针)传递给另一台机器。您可以使用GetBitmapBits函数来转换HBITMAP 转换为字节数组并使用 send() 发送该字节数组。
另一方面,您必须创建另一个兼容的位图,并使用 SetBitmapBits函数。
You can't pass an HBITMAP (which is a HANDLE, a reference pointer in memory) to another machine. You can use the GetBitmapBits Function to transform the HBITMAP into a byte array and send this byte array using send().
On the other side, you'll have to create another compatible bitmap, and use SetBitmapBits Function after the recv().
一般来说,发送原始内存是一个坏主意 - 即使你保证今天另一端总是有一台 Windows 机器,对于未来来说也不是很灵活 - 如果你想更改为不同的平台,甚至开始使用,该怎么办.NET 优于普通 win32?
我建议做的是将裁剪后的图像保存到 .bmp 文件中,然后像其他文件一样通过套接字发送该文件。这样您就可以对自己正在做的事情拥有最大的灵活性。
不幸的是,如果您使用 C++/win32 工作,输出位图有点像一场噩梦。这是我根据一些来源拼凑而成的输出位图的代码 - 它使用了我们的一些类成员,但您应该能够弄清楚如何在其中插入正确的值。
http://pastebin.com/gLw7ykMU
希望这有帮助!
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?
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. 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. 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
Hope this helps!