在 .NET 中组合多个 PNG8 图像的最简单方法
我正在尝试在 C# 中将一堆 8 位 PNG 图像组合成一个更大的 PNG 图像。奇怪的是,这似乎特别困难。
由于图形不支持索引颜色,因此您不能使用它,因此我尝试构建非索引位图(使用图形)并将其转换为索引颜色位图。转换很好,但我不知道如何设置输出图像的调色板。它默认为一些预定义的调色板,与我正在寻找的内容几乎没有关系。
那么:
有没有办法控制位图调色板?或者是否有另一种方法(例如 System.Windows.Media.Imaging.WriteableBitmap)可以支持此功能?
回复:WriteableBitmap:我似乎无法在网上找到任何关于如何在这种情况下组合 PNG 的示例,或者即使尝试它有任何意义。
I'm trying to combine a bunch of 8-bit PNG images into a larger PNG image in C#. Oddly enough, this seems to be particularly difficult.
Since Graphics does not support indexed color, you can't use that, so I tried building a non-indexed Bitmap (using Graphics) and converting that to indexed color bitmap. The conversion is fine, but I can't figure out how to set the palette of the output image. It defaults to some pre-defined palette which has little to do with what I'm looking for.
So:
Is there a way to control the bitmap palette? Or is there another method (System.Windows.Media.Imaging.WriteableBitmap, for example) which can support this?
Re: WriteableBitmap: I can't seem to find any examples online of how one would go about combining PNGs in this context, or even if it makes any sense to attempt it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明,我能够构建一个非索引位图并使用 PngBitmapEncoder 进行转换,如下所示:
加上辅助扩展方法:
请注意:PngBitmapEncoder 实际上似乎可以将 bpp 计数从 8 减少到 4。例如,当我使用 6 种颜色进行测试时,输出 PNG 仅是 4 位。当我使用色彩更丰富的图像时,它是 8 位的。到目前为止看起来像是一个功能......不过如果我能明确地控制它那就太好了。
It turns out that I was able to build out a non-indexed bitmap and convert using PngBitmapEncoder like so:
Plus the helper extension method:
Note for the wary: the PngBitmapEncoder actually seems to reduce the bpp count from 8 to 4 when it can. When I test with 6 colors, for example, the output PNG is only 4-bit. When I use a more color-rich image, it's 8-bit. Looks like a feature so far... though it would be nice if I had explicit control over it.
免责声明,我在 Atalasoft 工作。
我们的产品 DotImage Photo 是免费的,并且可以做到这一点。
读取 PNG
转换为 24 bpp
创建所需尺寸的图像
使用 OverlayCommand 将 img 覆盖到 img2 上
保存
如果您需要帮助,请对此答案发表评论或发表论坛条目。
Disclaimer, I work for Atalasoft.
Our product, DotImage Photo, is free and can do this.
To read a PNG
To convert to 24 bpp
Create an image of the size you want
use OverlayCommand to overlay img onto img2
To save
If you need help either comment on this answer or make a forum entry.