如何以索引格式保存图像并获取它的调色板? [英] How to save image in indexed format and get it's palette ?
问题描述
我想使用以下代码将图像保存为Format8bppIndexed:
I want to save image as Format8bppIndexed using this code :
Bitmap imgsource = new Bitmap(sourceimage);
Bitmap imgtarget = new Bitmap(imgsource.Width, imgsource.Height, PixelFormat.Format8bppIndexed);
for (int I = 0; I <= imgsource.Width - 1; I++)
{
for (int J = 0; J <= imgsource.Height - 1; J++)
{
imgtarget.SetPixel(I, J, imgsource.GetPixel(I, J));
}
}
imgtarget.Save(targetimage);
但我面临的错误是带有索引像素格式的图像不支持Setpixel
but I face error that "Setpixel is not supported for images with indexed pixel formats"
我希望用索引
保存图像我怎么做?
and I want to save image with indexed how I can do that ?
推荐答案
改为使用:
Bitmap imgtarget = imgsource.Clone(
new Rectangle(0, 0, imgsource.Width, imgsource.Height),
PixelFormat.Format8bppIndexed);
编辑:
有两种 Image
在GDI +中的:
位图
s 和 元文件
取值。通常从位图图像文件( .jpg
, .png
,)加载图像。 bmp
, .gif
, .exif
和 .tiff
)而不是图元文件( .wmf
或 .emf
)。因此,不是基于图像创建新的位图,而是将 Image
对象转换为位图
:
There are two kind of Image
s in GDI+:
Bitmap
s and Metafile
s. Usually you load the image from a bitmap image file (.jpg
, .png
, .bmp
, .gif
, .exif
and .tiff
) and not a metafile (.wmf
or .emf
). So, instead of creating a new bitmap based on the image, just cast the Image
object to Bitmap
:
Bitmap imgsource = (Bitmap)sourceimage;
代码的第一行,更改图像的origianl属性并将DIP重置为96。
The first line of your code, changes the origianl properties of the image and resets the DIP to 96.
这篇关于如何以索引格式保存图像并获取它的调色板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!