C# 将位图转换为索引颜色格式

发布于 2024-12-29 04:08:18 字数 188 浏览 2 评论 0原文

如何将 24 位颜色 System.Drawing.Bitmap 转换为索引(256 色)格式?我无法弄清楚如何计算调色板。我可以迭代像素并使用 int[] 来包含各种颜色,但当颜色超过 256 种时就会出现问题。有没有办法转换为索引格式并从 Bitmap 中提取 256 色调色板?

How can I convert a 24-bit colour System.Drawing.Bitmap to an indexed (256-colour) format? I'm having trouble working out how to calculate the palette. I can iterate over the pixels and use an int[] to contain the various colours but the problem comes when there are more than 256 colours. Is there a way to convert to an indexed format and extract a 256-colour palette from an Bitmap ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不再见 2025-01-05 04:08:18

使用位图克隆方法,您可以直接将源图像转换为 256 色调色板索引图像,如下所示:

Bitmap Result = Source.Clone(new Rectangle(0, 0, Source.Width, Source.Height), PixelFormat.Format8bppIndexed);

然后,如果您想访问调色板颜色,只需使用 Result.Palette.Entries 属性。

Using the Bitmap Clone Method you can directly convert the Source Image to a 256 color Palette Indexed image like this:

Bitmap Result = Source.Clone(new Rectangle(0, 0, Source.Width, Source.Height), PixelFormat.Format8bppIndexed);

Then if you want access the Palette Colors, just use the Result.Palette.Entries property.

小嗷兮 2025-01-05 04:08:18

我之前也遇到过同样的挑战。可以在.Net中使用GDI+来解决。

这篇文章对我帮助很大(包括示例):http://msdn.microsoft.com/ en-us/library/Aa479306

为了获得最佳质量,请使用“基于八叉树的量化”。

I had the same challenge earlier. It's possible to solve using GDI+ in .Net.

This article helped me a lot (including samples): http://msdn.microsoft.com/en-us/library/Aa479306

For best quality use "Octree-based Quantization".

温馨耳语 2025-01-05 04:08:18

WPF 可以访问 Windows 成像组件,从那里您可以使用 FormatConvertedBitmap 将图像转换为新的像素格式。 WIC 比 Vista 和 7 上的 System.Drawing 方法快得多,并且为您提供更多选择。

WPF has access to the Windows Imaging Component, from there you can use a FormatConvertedBitmap to convert the image to a new pixel format. WIC is much much faster than the System.Drawing methods on Vista and 7 and will allow you a lot more options.

不顾 2025-01-05 04:08:18

这不是内置的,但您可以使用外部 .NET 库来实现此目的,也可以使用 shell 调用 ImageMagic。

This is not built-in but you can either use external .NET libraries for this or shell out to the console to invoke ImageMagic.

宫墨修音 2025-01-05 04:08:18

一些帮助您入门的阅读材料。

  • Graphic Gems I 第 287-293 页,“颜色量化的简单方法:八叉树量化”

  • B.库尔兹。彩色显示器的最佳颜色量化。 IEEE 计算机视觉和模式识别会议论文集,1983 年,第 217-224 页。

  • Graphic Gems II 第 116-125 页,“高效逆色彩图计算”

本文介绍了一种将实际颜色映射到通过其他论文中描述的一些其他技术选择的简化颜色图。

  • Graphic Gems II 第 126-133 页,“最佳色彩量化的高效统计计算”

  • 吴晓林。通过动态规划和主分析进行颜色量化。 ACM 图形交易,卷。 11,第4期,1992年10月,第348-372页。

Some reading material to get you started.

  • Graphic Gems I pp. 287-293, "A Simple Method for Color Quantization: Octree Quantization"

  • B. Kurz. Optimal Color Quantization for Color Displays. Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition, 1983, pp. 217-224.

  • Graphic Gems II pp. 116-125, "Efficient Inverse Color Map Computation"

This paper describes an efficient technique to map actual colors to a reduced color map, selected by some other technique described in the other papers.

  • Graphic Gems II pp. 126-133, "Efficient Statistical Computations for Optimal Color Quantization"

  • Xiaolin Wu. Color Quantization by Dynamic Programming and Principal Analysis. ACM Transactions on Graphics, Vol. 11, No. 4, October 1992, pp 348-372.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文