Class ColorPaletteHelper
Namespace: Aspose.Imaging
Assembly: Aspose.Imaging.dll (25.2.0)
คลาสช่วยสำหรับการจัดการพาเลตสี
public static class ColorPaletteHelper
การสืบทอด
สมาชิกที่สืบทอด
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()
คืนค่า
พาเลตสี 4 บิต
Create4BitGrayscale(bool)
สร้างพาเลตสีเทา 4 บิต
public static IColorPalette Create4BitGrayscale(bool minIsWhite)
พารามิเตอร์
minIsWhite
bool
ถ้าตั้งค่าเป็น ```cstrue พาเลตจะเริ่มต้นด้วยสีขาว มิฉะนั้นจะเริ่มต้นด้วยสีดำ
คืนค่า
พาเลตสีเทา 4 บิต
Create8Bit()
สร้างพาเลตสี 8 บิต
public static IColorPalette Create8Bit()
คืนค่า
พาเลตสี 8 บิต
Create8BitGrayscale(bool)
สร้างพาเลตสีเทา 8 บิต
public static IColorPalette Create8BitGrayscale(bool minIsWhite)
พารามิเตอร์
minIsWhite
bool
ถ้าตั้งค่าเป็น ```cstrue พาเลตจะเริ่มต้นด้วยสีขาว มิฉะนั้นจะเริ่มต้นด้วยสีดำ
คืนค่า
พาเลตสีเทา 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);
// สร้างภาพ BMP ขนาด 100 x 100 px และบันทึกลงไฟล์
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
จำนวนบิต
คืนค่า
พาเลตสีเทา
ข้อยกเว้น
bits
CreateMonochrome()
สร้างพาเลตสีโมโนโครมที่มีเพียง 2 สีเท่านั้น
public static IColorPalette CreateMonochrome()
คืนค่า
พาเลตสีสำหรับภาพโมโนโครม
GetCloseImagePalette(RasterImage, int)
รับพาเลตสีจากภาพเรสเตอร์ (จัดพาเลตภาพ) ในกรณีที่ภาพไม่มีพาเลต ในกรณีที่มีพาเลตจะถูกใช้แทนการคำนวณ
public static IColorPalette GetCloseImagePalette(RasterImage image, int entriesCount)
พารามิเตอร์
image
RasterImage
ภาพเรสเตอร์
entriesCount
int
จำนวนรายการที่ต้องการ
คืนค่า
พาเลตสีที่เริ่มต้นด้วยสีที่พบบ่อยที่สุดจาก 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#]
// สร้างภาพ BMP ขนาด 100 x 100 px
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)
รับพาเลตสีจากภาพเรสเตอร์ (จัดพาเลตภาพ) ในกรณีที่ภาพไม่มีพาเลต พาเลตจะถูกปรับให้เหมาะสมเพื่อคุณภาพภาพที่ดีกว่าหรือใช้ “AS IS” เมื่อใช้ PaletteMiningMethod.UseCurrentPalette
public static IColorPalette GetCloseImagePalette(RasterImage image, int entriesCount, PaletteMiningMethod paletteMiningMethod)
พารามิเตอร์
image
RasterImage
ภาพเรสเตอร์
entriesCount
int
จำนวนรายการที่ต้องการ
paletteMiningMethod
PaletteMiningMethod
วิธีการทำเหมืองพาเลต
คืนค่า
พาเลตสีที่เริ่มต้นด้วยสีที่พบบ่อยที่สุดจาก 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
จำนวนรายการที่ต้องการ
คืนค่า
พาเลตสีที่เริ่มต้นด้วยสีที่พบบ่อยที่สุดจาก 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
ถ้าตั้งค่า จะใช้พาเลตภาพของตนเองถ้ามี
คืนค่า
พาเลตสีที่เริ่มต้นด้วยสีที่พบบ่อยที่สุดจาก 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
สีที่ควรใช้เป็นสีพื้นหลังสำหรับการแทนที่อัลฟาซีมโปร่งใส
คืนค่า
พาเลตสีที่เริ่มต้นด้วยสีที่พบบ่อยที่สุดจาก 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
สีที่ควรใช้เป็นสีพื้นหลังสำหรับการแทนที่อัลฟาซีมโปร่งใส
keepTransparency
bool
ถ้าตั้งค่า จะพิจารณาบิตช่องอัลฟาของสีในภาพ
คืนค่า
พาเลตสีที่เริ่มต้นด้วยสีที่พบบ่อยที่สุดจาก image
และมี entriesCount
รายการ
GetCloseTransparentImagePalette(RasterImage, int)
รับพาเลตสีจากภาพเรสเตอร์ (จัดพาเลตภาพ) ในกรณีที่ภาพไม่มีพาเลต ในกรณีที่มีพาเลตจะถูกใช้แทนการคำนวณ
public static IColorPalette GetCloseTransparentImagePalette(RasterImage image, int entriesCount)
พารามิเตอร์
image
RasterImage
ภาพเรสเตอร์
entriesCount
int
จำนวนรายการที่ต้องการ
คืนค่า
พาเลตสีที่เริ่มต้นด้วยสีที่พบบ่อยที่สุดจาก image
และมี entriesCount
รายการ
GetDownscalePalette(RasterImage)
รับพาเลตสี 256 สีที่ประกอบจากบิตบนของค่าสีเริ่มต้นของภาพ
public static ColorPalette GetDownscalePalette(RasterImage image)
พารามิเตอร์
image
RasterImage
ภาพ
คืนค่า
Aspose.Imaging.ColorPalette
GetUniformColorPalette(RasterImage)
รับพาเลตสี 256 สีที่เป็นเอกภาพ
public static ColorPalette GetUniformColorPalette(RasterImage image)
พารามิเตอร์
image
RasterImage
ภาพ
คืนค่า
Aspose.Imaging.ColorPalette
HasTransparentColors(IColorPalette)
ตรวจสอบว่าพาเลตที่กำหนดมีสีโปร่งใสหรือไม่
public static bool HasTransparentColors(IColorPalette palette)
พารามิเตอร์
palette
IColorPalette
พาเลต
คืนค่า
cstrue</code> หากพาเลตที่กำหนดมีสีโปร่งใส มิฉะนั้น
csfalse
ข้อยกเว้น
palette
เป็น null.