如何以索引格式保存图像并获取它的调色板? [英] How to save image in indexed format and get it's palette ?

查看:944
本文介绍了如何以索引格式保存图像并获取它的调色板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下代码将图像保存为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 Images in GDI+: Bitmaps and Metafiles. 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屋!

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