如何将XImage保存为PNG格式的base64字符串?
我正在 Ubuntu Linux 和 C++ 中进行开发。
我将桌面图像捕获到 XImage。
如何将XImage保存为PNG格式的base64字符串?
I'm developing in Ubuntu Linux and C++.
I captured the desktop image to XImage.
How to save XImage as base64 string in PNG format?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个将
XImage
数据转换为 jpeg 图像的 C 函数:我相信您可以轻松地将其放入您的代码中。您只需要使用适当的库来保存 PNG 文件(而不是 jpeg 文件)。
Here is a C function to convert
XImage
data into a jpeg image :I am sure you can easily fit this into your code. You just need to use appropriate library to save a PNG file (instead of a jpeg file).
我想说答案应该是使用 libpng。然而,在快速查看文档之后,我发现这个库的方式更多比需要的复杂(简单的事情应该很容易,困难的事情应该是可能的......在查看手册之后,似乎使用 libpng 的简单事情几乎是不可能的)。 PNG 格式有无数的选项,libpng 非常灵活且可插入,但显然当您只想做一些更简单的事情(例如保存单个不透明图像)时,没有简单的捷径。
我想我会倾向于以原始格式转储图像,然后使用 imagemagick (可能通过管道)。
另一方面,您是否考虑使用 Qt 而不是裸 X?捕获屏幕很容易,您可以以更易于管理的格式 (QImage) 获取屏幕。保存为几乎任何您喜欢的文件格式也很简单(但是 png 格式对 Qt 的 CPU 来说有点困难,因此如果您想进行实时视频捕获,则不建议这样做)。
I was about to say that the answer should be using libpng. However after a quick look at the documentation I find this library way much more complex than needed (easy things should be easy, hard things should be possible... after looking at the manual seems that easy things with libpng are instead close to impossible). PNG format has a gajillion options, libpng is extremely flexible and pluggable but apparently there is no easy shortcut when you just want to do something simpler (like saving a single opaque image).
I think I would be tempted to just dump the image in a raw format and then use imagemagick (possibly through a pipe).
On the other hand did you consider use Qt instead of bare X? Capturing the screen is easy and you can get it in a much more manageable format (QImage). Saving to almost any file format you like is also trivial (png format is however somewhat hard on CPU with Qt, so not recommended if you wanna do a live video capture).