Class ColorPaletteHelper

Class ColorPaletteHelper

Namespace: Aspose.Imaging
Assembly: Aspose.Imaging.dll (25.2.0)

用于颜色调色板操作的助手类。

public static class ColorPaletteHelper

继承

objectColorPaletteHelper

继承成员

object.GetType(), object.MemberwiseClone(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

方法

Create4Bit()

创建 4 位颜色调色板。

public static IColorPalette Create4Bit()

返回

IColorPalette

4 位颜色调色板。

Create4BitGrayscale(bool)

创建 4 位灰度调色板。

public static IColorPalette Create4BitGrayscale(bool minIsWhite)

参数

minIsWhite bool

如果设置为 ```cstrue,则调色板以白色开始,否则以黑色开始。

返回

IColorPalette

4 位灰度调色板。

Create8Bit()

创建 8 位颜色调色板。

public static IColorPalette Create8Bit()

返回

IColorPalette

8 位颜色调色板。

Create8BitGrayscale(bool)

创建 8 位灰度调色板。

public static IColorPalette Create8BitGrayscale(bool minIsWhite)

参数

minIsWhite bool

如果设置为 ```cstrue,则调色板以白色开始,否则以黑色开始。

返回

IColorPalette

8 位灰度调色板。

示例

以下示例创建一个调色板化的灰度 BMP 图像,然后将其保存到文件。```csharp [C#]

                                                                                                  string dir = "c:\\temp\\";

                                                                                                  Aspose.Imaging.ImageOptions.BmpOptions createOptions = new Aspose.Imaging.ImageOptions.BmpOptions();

                                                                                                  // 保存到文件
                                                                                                  createOptions.Source = new Aspose.Imaging.Sources.FileCreateSource(dir + "output.palette8bit.bmp", false);

                                                                                                  // 使用每像素 8 位来减小输出图像的大小。
                                                                                                  createOptions.BitsPerPixel = 8;

                                                                                                  // 设置标准的 8 位灰度颜色调色板,覆盖所有灰度颜色。
                                                                                                  // 如果处理的图像仅包含灰度颜色,则其调色板化版本
                                                                                                  // 在视觉上与非调色板化版本无区别。
                                                                                                  createOptions.Palette = Aspose.Imaging.ColorPaletteHelper.Create8BitGrayscale(false);

                                                                                                  // 不进行压缩保存。
                                                                                                  // 您也可以使用 RLE-8 压缩来减小输出图像的大小。
                                                                                                  createOptions.Compression = Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rgb;

                                                                                                  // 设置水平和垂直分辨率为 96 dpi。
                                                                                                  createOptions.ResolutionSettings = new Aspose.Imaging.ResolutionSetting(96.0, 96.0);

                                                                                                  // 创建 100 x 100 像素的 BMP 图像并保存到文件。
                                                                                                  using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Create(createOptions, 100, 100))
                                                                                                  {
                                                                                                      Aspose.Imaging.Graphics graphics = new Aspose.Imaging.Graphics(image);

                                                                                                      Aspose.Imaging.Brushes.LinearGradientBrush gradientBrush = new Aspose.Imaging.Brushes.LinearGradientBrush(
                                                                                                          new Aspose.Imaging.Point(0, 0),
                                                                                                          new Aspose.Imaging.Point(image.Width, image.Height),
                                                                                                          Aspose.Imaging.Color.Black,
                                                                                                          Aspose.Imaging.Color.White);

                                                                                                      // 用灰度渐变填充图像
                                                                                                      graphics.FillRectangle(gradientBrush, image.Bounds);

                                                                                                      image.Save();
                                                                                                  }

### <a id="Aspose_Imaging_ColorPaletteHelper_CreateGrayscale_System_Int32_"></a> CreateGrayscale\(int\)

获取指定位数的灰度调色板。允许的位值为 1、2、4、8。

```csharp
public static IColorPalette CreateGrayscale(int bits)

参数

bits int

位数。

返回

IColorPalette

灰度调色板。

异常

ArgumentOutOfRangeException

bits

CreateMonochrome()

创建仅包含 2 种颜色的单色调色板。

public static IColorPalette CreateMonochrome()

返回

IColorPalette

单色图像的颜色调色板。

GetCloseImagePalette(RasterImage, int)

从光栅图像获取颜色调色板(调色板化图像),如果图像没有调色板。如果存在调色板,将使用它,而不是进行计算。

public static IColorPalette GetCloseImagePalette(RasterImage image, int entriesCount)

参数

image RasterImage

光栅图像。

entriesCount int

所需的条目数量。

返回

IColorPalette

颜色调色板,从 image 中最常见的颜色开始,并包含 entriesCount 个条目。

示例

以下示例加载一个 BMP 图像,并使用各种保存选项将其保存回 BMP。```csharp [C#]

                                                                                                   string dir = "c:\\temp\\";

                                                                                                   using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.bmp"))
                                                                                                   {
                                                                                                       Aspose.Imaging.RasterImage rasterImage = (Aspose.Imaging.RasterImage)image;

                                                                                                       // 创建 BmpOptions
                                                                                                       Aspose.Imaging.ImageOptions.BmpOptions saveOptions = new Aspose.Imaging.ImageOptions.BmpOptions();

                                                                                                       // 使用每像素 8 位来减小输出图像的大小。
                                                                                                       saveOptions.BitsPerPixel = 8;

                                                                                                       // 设置最接近的 8 位颜色调色板,覆盖尽可能多的图像像素,以便调色板化图像
                                                                                                       // 在视觉上几乎与非调色板化图像无区别。
                                                                                                       saveOptions.Palette = Aspose.Imaging.ColorPaletteHelper.GetCloseImagePalette(rasterImage, 256);

                                                                                                       // 不进行压缩保存。
                                                                                                       // 您也可以使用 RLE-8 压缩来减小输出图像的大小。
                                                                                                       saveOptions.Compression = Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rgb;

                                                                                                       // 设置水平和垂直分辨率为 96 dpi。
                                                                                                       saveOptions.ResolutionSettings = new Aspose.Imaging.ResolutionSetting(96.0, 96.0);

                                                                                                       image.Save(dir + "sample.bmpoptions.bmp", saveOptions);
                                                                                                   }

以下示例演示如何将 BMP 图像调色板化以减小其输出大小。```csharp
[C#]

                                                                                              // 创建 100 x 100 像素的 BMP 图像。
                                                                                              using (Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(100, 100))
                                                                                              {
                                                                                                  // 从图像的左上角到右下角的线性渐变。
                                                                                                  Aspose.Imaging.Brushes.LinearGradientBrush brush =
                                                                                                      new Aspose.Imaging.Brushes.LinearGradientBrush(
                                                                                                          new Aspose.Imaging.Point(0, 0),
                                                                                                          new Aspose.Imaging.Point(bmpImage.Width, bmpImage.Height),
                                                                                                          Aspose.Imaging.Color.Red,
                                                                                                          Aspose.Imaging.Color.Green);

                                                                                                  // 用线性渐变画刷填充整个图像。
                                                                                                  Aspose.Imaging.Graphics gr = new Aspose.Imaging.Graphics(bmpImage);
                                                                                                  gr.FillRectangle(brush, bmpImage.Bounds);

                                                                                                  // 获取最接近的 8 位颜色调色板,覆盖尽可能多的像素,以便调色板化图像
                                                                                                  // 在视觉上几乎与非调色板化图像无区别。
                                                                                                  Aspose.Imaging.IColorPalette palette = Aspose.Imaging.ColorPaletteHelper.GetCloseImagePalette(bmpImage, 256);

                                                                                                  // 8 位调色板最多包含 256 种颜色。
                                                                                                  Aspose.Imaging.ImageOptions.BmpOptions saveOptions = new Aspose.Imaging.ImageOptions.BmpOptions();
                                                                                                  saveOptions.Palette = palette;
                                                                                                  saveOptions.BitsPerPixel = 8;

                                                                                                  using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
                                                                                                  {
                                                                                                      bmpImage.Save(stream, saveOptions);
                                                                                                      System.Console.WriteLine("调色板化图像的大小为 {0} 字节。", stream.Length);
                                                                                                  }

                                                                                                  using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
                                                                                                  {
                                                                                                      bmpImage.Save(stream);
                                                                                                      System.Console.WriteLine("非调色板化图像的大小为 {0} 字节。", stream.Length);
                                                                                                  }
                                                                                              }

                                                                                              // 输出如下:
                                                                                              // 调色板化图像的大小为 11078 字节。
                                                                                              // 非调色板化图像的大小为 40054 字节。

GetCloseImagePalette(RasterImage, int, PaletteMiningMethod)

从光栅图像获取颜色调色板(调色板化图像),如果图像没有调色板。当使用 PaletteMiningMethod.UseCurrentPalette 时,调色板将被优化以提高索引图像的质量或按原样使用。

public static IColorPalette GetCloseImagePalette(RasterImage image, int entriesCount, PaletteMiningMethod paletteMiningMethod)

参数

image RasterImage

光栅图像。

entriesCount int

所需的条目数量。

paletteMiningMethod PaletteMiningMethod

调色板挖掘方法。

返回

IColorPalette

颜色调色板,从 image 中最常见的颜色开始,并包含 entriesCount 个条目。

示例

以下示例演示如何压缩 PNG 图像,使用最佳适配调色板的索引颜色。```csharp [C#]

                                                                                                         // 加载 png 图像        
                                                                                                             string  sourceFilePath="OriginalRings.png";
                                                                                                             string  outputFilePath="OriginalRingsOutput.png";
                                                                                                             using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(sourceFilePath))
                                                                                                         {
                                                                                                             image.Save(outputFilePath, new Aspose.Imaging.ImageOptions.PngOptions()
                                                                                                             {
                                                                                                                  Progressive = true,
                                                                                                                      // 使用索引颜色类型
                                                                                                                  ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.IndexedColor,
                                                                                                                      // 使用最大压缩
                                                                                                                  CompressionLevel = 9,
                                                                                                               // 获取最接近的 8 位颜色调色板,覆盖尽可能多的像素,以便调色板化图像
                                                                                                                  // 在视觉上几乎与非调色板化图像无区别。
                                                                                                                  Palette = Aspose.Imaging.ColorPaletteHelper.GetCloseImagePalette((Aspose.Imaging.RasterImage)image, 256, Aspose.Imaging.PaletteMiningMethod.Histogram)
                                                                                                             });
                                                                                                         }
                                                                                                             // 输出文件大小应显著减少

### <a id="Aspose_Imaging_ColorPaletteHelper_GetCloseImagePalette_Aspose_Imaging_RasterImage_Aspose_Imaging_Rectangle_System_Int32_"></a> GetCloseImagePalette\(RasterImage, Rectangle, int\)

从光栅图像获取颜色调色板(调色板化图像),如果图像没有调色板。如果存在调色板,将使用它,而不是进行计算。

```csharp
public static IColorPalette GetCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount)

参数

image RasterImage

光栅图像。

destBounds Rectangle

目标图像边界。

entriesCount int

所需的条目数量。

返回

IColorPalette

颜色调色板,从 image 中最常见的颜色开始,并包含 entriesCount 个条目。

GetCloseImagePalette(RasterImage, Rectangle, int, bool)

从光栅图像获取颜色调色板(调色板化图像),如果图像没有调色板。如果存在调色板,将使用它,而不是进行计算。

public static IColorPalette GetCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount, bool useImagePalette)

参数

image RasterImage

光栅图像。

destBounds Rectangle

目标图像边界。

entriesCount int

所需的条目数量。

useImagePalette bool

如果设置,它将使用自己的图像调色板(如果可用)。

返回

IColorPalette

颜色调色板,从 image 中最常见的颜色开始,并包含 entriesCount 个条目。

GetCloseImagePalette(RasterImage, Rectangle, int, bool, Color)

从光栅图像获取颜色调色板(调色板化图像),如果图像没有调色板。如果存在调色板,将使用它,而不是进行计算。

public static IColorPalette GetCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount, bool useImagePalette, Color alphaBlendInColor)

参数

image RasterImage

光栅图像。

destBounds Rectangle

目标图像边界。

entriesCount int

所需的条目数量。

useImagePalette bool

如果设置,它将使用自己的图像调色板(如果可用)。

alphaBlendInColor Color

应作为半透明 alpha 替换的背景色。

返回

IColorPalette

颜色调色板,从 image 中最常见的颜色开始,并包含 entriesCount 个条目。

GetCloseImagePalette(RasterImage, Rectangle, int, bool, Color, bool)

从光栅图像获取颜色调色板(调色板化图像),如果图像没有调色板。如果存在调色板,将使用它,而不是进行计算。

public static IColorPalette GetCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount, bool useImagePalette, Color alphaBlendInColor, bool keepTransparency)

参数

image RasterImage

光栅图像。

destBounds Rectangle

目标图像边界。

entriesCount int

所需的条目数量。

useImagePalette bool

如果设置,它将使用自己的图像调色板(如果可用)。

alphaBlendInColor Color

应作为半透明 alpha 替换的背景色。

keepTransparency bool

如果设置,它将考虑图像颜色的 alpha 通道位。

返回

IColorPalette

颜色调色板,从 image 中最常见的颜色开始,并包含 entriesCount 个条目。

GetCloseTransparentImagePalette(RasterImage, int)

从光栅图像获取颜色调色板(调色板化图像),如果图像没有调色板。如果存在调色板,将使用它,而不是进行计算。

public static IColorPalette GetCloseTransparentImagePalette(RasterImage image, int entriesCount)

参数

image RasterImage

光栅图像。

entriesCount int

所需的条目数量。

返回

IColorPalette

颜色调色板,从 image 中最常见的颜色开始,并包含 entriesCount 个条目。

GetDownscalePalette(RasterImage)

获取由初始图像颜色值的上位位组成的 256 色调色板。

public static ColorPalette GetDownscalePalette(RasterImage image)

参数

image RasterImage

图像。

返回

ColorPalette

Aspose.Imaging.ColorPalette。

GetUniformColorPalette(RasterImage)

获取均匀的 256 色调色板。

public static ColorPalette GetUniformColorPalette(RasterImage image)

参数

image RasterImage

图像。

返回

ColorPalette

Aspose.Imaging.ColorPalette。

HasTransparentColors(IColorPalette)

确定指定的调色板是否具有透明颜色。

public static bool HasTransparentColors(IColorPalette palette)

参数

palette IColorPalette

调色板。

返回

bool

cstrue</code> 如果指定的调色板具有透明颜色;否则,csfalse。

异常

ArgumentNullException

palette 为 null。

 中文