【问题标题】:How to save image in indexed format and get it's palette ?如何以索引格式保存图像并获取它的调色板?
【发布时间】:2013-02-24 17:08:19
【问题描述】:

我想使用此代码将图像保存为 Format8bppIndexed:

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”的错误

我想用索引保存图像 我该怎么做?

【问题讨论】:

    标签: c# image-processing


    【解决方案1】:

    改用这个:

    Bitmap imgtarget = imgsource.Clone(
        new Rectangle(0, 0, imgsource.Width, imgsource.Height),
        PixelFormat.Format8bppIndexed);
    

    编辑:

    GDI+中有两种ImagesBitmapsMetafiles。通常您从位图图像文件(.jpg.png.bmp.gif.exif.tiff)而不是元文件(.wmf.emf)加载图像。因此,不要基于图像创建新的位图,只需将 Image 对象转换为 Bitmap

    Bitmap imgsource = (Bitmap)sourceimage;
    

    代码的第一行,更改图像的原始属性并将 DIP 重置为 96。

    【讨论】:

    • 它现在可以工作了,但是输出图像是 96 dpi 和 8 位深度,输入是 300 dpi 和 32 位深度
    • @MohamedKamal 查看我的编辑。代码的第一行,将 DIP 更改为 96。你想要一个 8 位深度的图像,对吧?
    • 我可以使用“SetResolution”类更改 dpi ...并希望将位深度更改为 32
    • PixelFormat.Format8bppIndexed 怎么样? 8bpp 表示每像素 8 位。它是图像的位深度:8.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    • 2014-03-12
    • 2018-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多