问题描述
我能否仅使用 Platform SDK 将内存中的位图转换为 PNG(即不写入文件)?(即没有 libpng 等).
Can I convert a bitmap to PNG in memory (i.e. without writing to a file) using only the Platform SDK? (i.e. no libpng, etc.).
我还希望能够为此图像定义透明颜色(不是 Alpha 通道).
I also want to be able to define a transparent color (not alpha channel) for this image.
GdiPlus 解决方案似乎仅限于宽度可被 4 整除的图像.在调用 Save() 期间,其他任何事情都失败了.有谁知道此限制的原因以及我如何/是否可以解决它?
The GdiPlus solution seems to be limited to images of width divisible by 4. Anything else fails during the call to Save(). Does anyone know the reason for this limitation and how/whether I can work around it?
更新:赏金
我开始赏金(我真的希望它起作用).我实现了 GDI+ 解决方案,但正如我所说,它仅限于四边形宽度的图像.任何能够解决此宽度问题(无需更改图像尺寸)或可以提供替代的非 GDI+ 解决方案的人都会获得奖励.
I'm starting a bounty (I really want this to work). I implemented the GDI+ solution, but as I said, it's limited to images with quad width. The bounty will go to anyone who can solve this width issue (without changing the image dimensions), or can offer an alternative non-GDI+ solution that works.
推荐答案
我使用 libpng 并且它似乎可以处理我扔给它的所有东西(我已经在单元测试中使用它来处理 257x255 图像之类的东西,它们不会造成任何问题).我相信 API 足够灵活,不会被束缚到文件 I/O(或者至少你可以覆盖它的默认行为,例如在 )
I read and write PNGs using libpng and it seems to deal with everthing I throw at it (I've used it in unit-tests with things like 257x255 images and they cause no trouble). I believe the API is flexible enough to not be tied to file I/O (or at least you can override its default behaviour e.g see png_set_write_fn
in section on customization)
实际上我总是通过更干净的使用它boost::gil PNG IO 扩展,但不幸的是,这需要char*
文件名,如果你深入研究它的实现中的 png_writer
和 file_mgr
类,它似乎与 FILE*
紧密相关>(尽管如果您在 Linux 上使用 fmemopen 和内存缓冲区的版本可能很容易制作).
In practice I always use it via the much cleaner boost::gil PNG IO extension, but unfortunately that takes char*
filenames and if you dig into it the png_writer
and file_mgr
classes in its implementation it seem pretty tied to FILE*
(although if you were on Linux a version using fmemopen and in-memory buffers could probably be cooked up quite easily).
这篇关于在 C++ (win32) 中将位图转换为内存中的 PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!