C# 调整索引颜色位图的大小并维护颜色

发布于 2024-12-29 16:03:05 字数 693 浏览 2 评论 0原文

我有一个 256 色索引格式的位图,我需要调整它的大小,因此我创建了一个 24 位 RGB 格式的新位图,并使用 < code>Graphics 对象,因为我无法从索引颜色位图创建图形对象。然后,我需要将调整大小的图像另存为索引颜色格式,因此我使用 FormatConvertedBitmap 转换为索引颜色,如下所示:

BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
FormatConvertedBitmap converted = new FormatConvertedBitmap();
converted.BeginInit();
converted.Source = bitmapSource;
converted.DestinationFormat = System.Windows.Media.PixelFormats.Indexed8;
converted.DestinationPalette = new BitmapPalette(bitmapSource, 256);
converted.EndInit();

这可行,但纯色现在呈颗粒状并包含其他颜色像素。是否有更好的方法来调整索引彩色图像的大小或保持纯色?

I have a Bitmap which is in 256-colour indexed format, I need to resize it so I create a new Bitmap in 24-bit RGB format and draw it using a Graphics object as I cannot create a graphics object from an indexed colour bitmap. I then need to save the resized image back as an indexed colour format so I use FormatConvertedBitmap to convert to indexed colour like this:

BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
FormatConvertedBitmap converted = new FormatConvertedBitmap();
converted.BeginInit();
converted.Source = bitmapSource;
converted.DestinationFormat = System.Windows.Media.PixelFormats.Indexed8;
converted.DestinationPalette = new BitmapPalette(bitmapSource, 256);
converted.EndInit();

This works but the solid colours are now grainy and contain other colour pixels. Is there a better method to either resize an indexed colour image or to maintain the solid colours?

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

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

发布评论

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

评论(1

轮廓§ 2025-01-05 16:03:05

是的,这是不可避免的。您想要控制以影响此效果的核心属性是 Graphics.InterpolationMode。像双三次这样的高质量选择将为调整大小的图像添加很多颜色。尝试将其恢复为 256 色将会产生颗粒状图像。您可以使用 NearestNeighbor 来减少添加颜色的数量,但最终会得到块状图像。

您可以尝试 GIF 编码器,它也会强制将图像重新采样回 256 色。它使用抖动算法。但往往会产生斑点伪影,当图像具有大面积的相似颜色(例如蓝天)时,这些伪影就会变得明显。除了不重新采样之外,没有什么神奇的治疗方法。对于我们如今拥有的出色硬件来说,这样做是没有意义的。

Yes, this is pretty inevitable. The core property you want to control to influence this is Graphics.InterpolationMode. A high quality selection like Bicubic will add a lot of colors to the resized image. Trying to whack it back to 256 colors is going to produce a grainy image. You could use NearestNeighbor instead to reduce the number of added colors but you'll end up with a blocky image.

You could try the GIF encoder, it is forced to resample the image back to 256 colors as well. It uses a dithering algorithm. But tends to produce flecky artifacts that become noticeable when the image has large areas of similar colors, like a blue sky. There is no magical cure beyond simply not resampling back. There is just no point in doing that with the wonderful hardware we have these days.

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