C# - 图像复制到8位索引图像 [英] C# - Copy an Image into an 8-bit Indexed Image

查看:444
本文介绍了C# - 图像复制到8位索引图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个普通的32位图像对象创建一个8位索引图像。

I want to create an 8-bit indexed image from a regular 32-bit Image object.

Bitmap img = new Bitmap(imgPath); // 32-bit
Bitmap img8bit = new Bitmap(imgW, imgH, Format8bppIndexed); // 8-bit

// copy img to img8bit -- HOW?

img8bit.Save(imgNewPath, ImageFormat.Png);

我不能使用的setPixel到它在逐个像素复制,因为图形不与索引图像的工作

I cannot use SetPixel to copy it over pixel-by-pixel since Graphics doesn't work with indexed images.

怎么回事?

推荐答案

我发现了一个 C#库该转换的位图成调色板(8位)的图像。该技术是快速的,因为它直接调用GDI32(Windows图形系统)。

I found a C# library that converts a bitmap into a palettized (8-bit) image. The technique is fast because it calls GDI32 (the windows graphics system) directly.

要转换为8bpp(调色板)用灰度调色板图像,执行

To convert to an 8bpp (palettized) image with a greyscale palette, do

System.Drawing.Bitmap b0 = CopyToBpp(b,8);

如果你想转换为图像着不同的色调,看看CopyToBpp源$ C ​​$ c代表建议的意见。需要注意的是,当你转换为1bpp或8bpp调色板副本时,Windows将着眼于每一个像素一个接一个,并将选择调色板条目是最接近像素。根据您的源图像和调色板的选择上,你很可能与仅使用颜色可供一半调色板中生成的图像结束了。

If you want to convert to an image with a different palette, look at the comments in the source code of CopyToBpp for suggestions. Note that, when you convert to a 1bpp or 8bpp palettized copy, Windows will look at each pixel one by one, and will chose the palette entry that's closest to that pixel. Depending on your source image and choice of palette, you may very well end up with a resulting image that uses only half of the colours available in the palette.

这篇关于C# - 图像复制到8位索引图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆