Class ColorPaletteHelper
名称: Aspose.Imaging 收藏: Aspose.Imaging.dll (25.4.0)
助理类对颜色板操纵。
public static class ColorPaletteHelper
Inheritance
继承人
object.GetType() , object.MemberwiseClone() , object.ToString() , object.Equals(object?) , object.Equals(object?, object?) , object.ReferenceEquals(object?, object?) , object.GetHashCode()
Methods
Create4Bit()
创建4位彩色板。
public static IColorPalette Create4Bit()
Returns
4位彩色板。
Create4BitGrayscale(博尔)
创建4位灰色板块。
public static IColorPalette Create4BitGrayscale(bool minIsWhite)
Parameters
minIsWhite
bool
如果设置为“真实”,板块以白色开始,否则它以黑色开始。
Returns
四位格雷斯卡尔板。
Create8Bit()
创建8位颜色板。
public static IColorPalette Create8Bit()
Returns
8位彩色板。
Create8BitGrayscale(博尔)
创建8位灰色板块。
public static IColorPalette Create8BitGrayscale(bool minIsWhite)
Parameters
minIsWhite
bool
如果设置为“真实”,板块以白色开始,否则它以黑色开始。
Returns
8位灰色板。
Examples
下面的例子创建了一个模糊的灰色 BMP 图像,然后将其保存到文件中。
string dir = "c:\\temp\\";
Aspose.Imaging.ImageOptions.BmpOptions createOptions = new Aspose.Imaging.ImageOptions.BmpOptions();
// Save to a file
createOptions.Source = new Aspose.Imaging.Sources.FileCreateSource(dir + "output.palette8bit.bmp", false);
// Use 8 bits per pixel to reduce the size of the output image.
createOptions.BitsPerPixel = 8;
// Set the standard 8-bit grayscale color palette which covers all grayscale colors.
// If the processed image contains only grayscale colors, then its palettized version
// is visually indistinguishable from a non-palletized one.
createOptions.Palette = Aspose.Imaging.ColorPaletteHelper.Create8BitGrayscale(false);
// Save without compression.
// You can also use RLE-8 compression to reduce the size of the output image.
createOptions.Compression = Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rgb;
// Set the horizontal and vertical resolution to 96 dpi.
createOptions.ResolutionSettings = new Aspose.Imaging.ResolutionSetting(96.0, 96.0);
// Create a BMP image of 100 x 100 px and save it to a file.
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);
// Fill the image with a grayscale gradient
graphics.FillRectangle(gradientBrush, image.Bounds);
image.Save();
}
CreateGrayscale(因特)
得到指定的比特数的灰色分数,允许的比特值为1,2,4,8。
public static IColorPalette CreateGrayscale(int bits)
Parameters
bits
int
小小的计算。
Returns
格雷斯卡尔板
Exceptions
bits'
CreateMonochrome()
创建一个单色颜色板,仅含有2种颜色。
public static IColorPalette CreateMonochrome()
Returns
色彩板为单色图像。
GetCloseImagePalette(拉斯特图像, int)
收到的颜色板从拉斯特图像(板图像),如果图像没有一个,如果板存在,它将被用来进行计算。
public static IColorPalette GetCloseImagePalette(RasterImage image, int entriesCount)
Parameters
image
RasterImage
拉斯特图像。
entriesCount
int
所需的输入数。
Returns
颜色板从 image’ 最常见的颜色开始,并包含
entriesCount’ 输入。
Examples
下面的例子加载了 BMP 图像,并使用各种存储选项将其恢复到 BMP。
string dir = "c:\\temp\\";
using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Load(dir + "sample.bmp"))
{
Aspose.Imaging.RasterImage rasterImage = (Aspose.Imaging.RasterImage)image;
// Create BmpOptions
Aspose.Imaging.ImageOptions.BmpOptions saveOptions = new Aspose.Imaging.ImageOptions.BmpOptions();
// Use 8 bits per pixel to reduce the size of the output image.
saveOptions.BitsPerPixel = 8;
// Set the closest 8-bit color palette which covers the maximal number of image pixels, so that a palettized image
// is almost visually indistinguishable from a non-palletized one.
saveOptions.Palette = Aspose.Imaging.ColorPaletteHelper.GetCloseImagePalette(rasterImage, 256);
// Save without compression.
// You can also use RLE-8 compression to reduce the size of the output image.
saveOptions.Compression = Aspose.Imaging.FileFormats.Bmp.BitmapCompression.Rgb;
// Set the horizontal and vertical resolution to 96 dpi.
saveOptions.ResolutionSettings = new Aspose.Imaging.ResolutionSetting(96.0, 96.0);
image.Save(dir + "sample.bmpoptions.bmp", saveOptions);
}
下面的例子表明如何塑造 BMP 图像以减少其输出大小。
// Create a BMP image 100 x 100 px.
using (Aspose.Imaging.FileFormats.Bmp.BmpImage bmpImage = new Aspose.Imaging.FileFormats.Bmp.BmpImage(100, 100))
{
// The linear gradient from the left-top to the right-bottom corner of the image.
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);
// Fill the entire image with the linear gradient brush.
Aspose.Imaging.Graphics gr = new Aspose.Imaging.Graphics(bmpImage);
gr.FillRectangle(brush, bmpImage.Bounds);
// Get the closest 8-bit color palette which covers as many pixels as possible, so that a palettized image
// is almost visually indistinguishable from a non-palletized one.
Aspose.Imaging.IColorPalette palette = Aspose.Imaging.ColorPaletteHelper.GetCloseImagePalette(bmpImage, 256);
// 8-bit palette contains at most 256 colors.
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("The palettized image size is {0} bytes.", stream.Length);
}
using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
bmpImage.Save(stream);
System.Console.WriteLine("The non-palettized image size is {0} bytes.", stream.Length);
}
}
// The output looks like this:
// The palettized image size is 11078 bytes.
// The non-palettized image size is 40054 bytes.
GetCloseImagePalette(RasterImage, int, PaletteMiningMethod)
如果图像没有一个,则将从拉斯特图像(palletizes图像)中获取颜色板,该板即将优化为更好的索引图像质量或在使用 PaletteMiningMethod.UseCurrentPalette时采取“AS IS”。
public static IColorPalette GetCloseImagePalette(RasterImage image, int entriesCount, PaletteMiningMethod paletteMiningMethod)
Parameters
image
RasterImage
拉斯特图像。
entriesCount
int
所需的输入数。
paletteMiningMethod
PaletteMiningMethod
采矿方法。
Returns
颜色板从 image’ 最常见的颜色开始,并包含
entriesCount’ 输入。
Examples
下面的例子显示如何压缩PNG图像,使用最适合的色彩
// Loads png image
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,
// Use indexed color type
ColorType = Aspose.Imaging.FileFormats.Png.PngColorType.IndexedColor,
// Use maximal compression
CompressionLevel = 9,
// Get the closest 8-bit color palette which covers as many pixels as possible, so that a palettized image
// is almost visually indistinguishable from a non-palletized one.
Palette = Aspose.Imaging.ColorPaletteHelper.GetCloseImagePalette((Aspose.Imaging.RasterImage)image, 256, Aspose.Imaging.PaletteMiningMethod.Histogram)
});
}
// The output file size should be significantly reduced
GetCloseImagePalette(RasterImage, 直角, int)
收到的颜色板从拉斯特图像(板图像),如果图像没有一个,如果板存在,它将被用来进行计算。
public static IColorPalette GetCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount)
Parameters
image
RasterImage
拉斯特图像。
destBounds
Rectangle
目的地图像限制。
entriesCount
int
所需的输入数。
Returns
颜色板从 image’ 最常见的颜色开始,并包含
entriesCount’ 输入。
GetCloseImagePalette(RasterImage, 直角, int, bool)
收到的颜色板从拉斯特图像(板图像),如果图像没有一个,如果板存在,它将被用来进行计算。
public static IColorPalette GetCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount, bool useImagePalette)
Parameters
image
RasterImage
拉斯特图像。
destBounds
Rectangle
目的地图像限制。
entriesCount
int
所需的输入数。
useImagePalette
bool
如果设置,它将使用自己的图像板,如果可用
Returns
颜色板从 image’ 最常见的颜色开始,并包含
entriesCount’ 输入。
GetCloseImagePalette(RasterImage, Rectangle, int, bool, 色彩)
收到的颜色板从拉斯特图像(板图像),如果图像没有一个,如果板存在,它将被用来进行计算。
public static IColorPalette GetCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount, bool useImagePalette, Color alphaBlendInColor)
Parameters
image
RasterImage
拉斯特图像。
destBounds
Rectangle
目的地图像限制。
entriesCount
int
所需的输入数。
useImagePalette
bool
如果设置,它将使用自己的图像板,如果可用
alphaBlendInColor
Color
该颜色应该作为半透明的阿尔法替代的背景颜色。
Returns
颜色板从 image’ 最常见的颜色开始,并包含
entriesCount’ 输入。
GetCloseImagePalette(RasterImage, Rectangle, int, bool, 颜色, bool)
收到的颜色板从拉斯特图像(板图像),如果图像没有一个,如果板存在,它将被用来进行计算。
public static IColorPalette GetCloseImagePalette(RasterImage image, Rectangle destBounds, int entriesCount, bool useImagePalette, Color alphaBlendInColor, bool keepTransparency)
Parameters
image
RasterImage
拉斯特图像。
destBounds
Rectangle
目的地图像限制。
entriesCount
int
所需的输入数。
useImagePalette
bool
如果设置,它将使用自己的图像板,如果可用
alphaBlendInColor
Color
该颜色应该作为半透明的阿尔法替代的背景颜色。
keepTransparency
bool
如果设置,它将考虑图像颜色的阿尔法频道比特。
Returns
颜色板从 image’ 最常见的颜色开始,并包含
entriesCount’ 输入。
GetCloseTransparentImagePalette(拉斯特图像, int)
收到的颜色板从拉斯特图像(板图像),如果图像没有一个,如果板存在,它将被用来进行计算。
public static IColorPalette GetCloseTransparentImagePalette(RasterImage image, int entriesCount)
Parameters
image
RasterImage
拉斯特图像。
entriesCount
int
所需的输入数。
Returns
颜色板从 image’ 最常见的颜色开始,并包含
entriesCount’ 输入。
GetDownscalePalette(RasterImage)
获取 256 色彩板,由初始图像颜色值的顶部比特组成。
public static ColorPalette GetDownscalePalette(RasterImage image)
Parameters
image
RasterImage
图像。
Returns
色彩 色彩 色彩 色彩 色彩 色彩
GetUniformColorPalette(RasterImage)
256 彩色彩色彩彩色彩色彩色彩色彩
public static ColorPalette GetUniformColorPalette(RasterImage image)
Parameters
image
RasterImage
图像。
Returns
色彩 色彩 色彩 色彩 色彩 色彩
HasTransparentColors(色彩色彩)
确定指定的板是否具有透明的颜色。
public static bool HasTransparentColors(IColorPalette palette)
Parameters
palette
IColorPalette
帕莱特。
Returns
“真实”如果指定的板有透明的颜色;否则,“虚假”。
Exceptions
palette’ is null.