Chuyển đổi giữa các Chế độ Màu khác nhau trong PSD

Bạn có thể tìm hiểu về các chế độ màu được hỗ trợ trong Aspose.PSD từ bài viết này.

Aspose.PSD, ví dụ, hỗ trợ chuyển đổi từ 8 sang 16 bit mỗi pixel và ngược lại. Chuyển đổi các hồ sơ ICC CMYK và chuyển đổi khác từ một độ sâu bit loại sang một loại khác. Dưới đây là các ví dụ về cách chuyển đổi thành Ảnh xám trong Tệp PSD.

Chuyển đổi từ CMYK, RGB, Ảnh xám sang Chế độ Màu Ảnh xám

Mã mẫu được cung cấp dưới đây minh họa cách thực hiện chuyển đổi CMYK, RGB hoặc Ảnh xám mà không cần sử dụng Photoshop.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
//The following example demonstrates that reading and saving the Grayscale 16 bit PSD files works correctly and without an exception.
void SaveToPsdThenLoadAndSaveToPng(
string file,
ColorModes colorMode,
short channelBitsCount,
short channelsCount,
CompressionMethod compression,
int layerNumber)
{
string filePath = file + ".psd";
string postfix = colorMode.ToString() + channelBitsCount + "_" + channelsCount + "_" + compression;
string exportPath = file + postfix + ".psd";
PsdOptions psdOptions = new PsdOptions()
{
ColorMode = colorMode,
ChannelBitsCount = channelBitsCount,
ChannelsCount = channelsCount,
CompressionMethod = compression
};
using (PsdImage image = (PsdImage)Image.Load(filePath))
{
RasterCachedImage raster = layerNumber >= 0 ? (RasterCachedImage)image.Layers[layerNumber] : image;
Graphics graphics = new Graphics(raster);
int width = raster.Width;
int height = raster.Height;
Rectangle rect = new Rectangle(
width / 3,
height / 3,
width - (2 * (width / 3)) - 1,
height - (2 * (height / 3)) - 1);
graphics.DrawRectangle(new Pen(Color.DarkGray, 1), rect);
image.Save(exportPath, psdOptions);
}
string pngExportPath = Path.ChangeExtension(exportPath, "png");
using (PsdImage image = (PsdImage)Image.Load(exportPath))
{
// Here should be no exception.
image.Save(pngExportPath, new PngOptions() { ColorType = PngColorType.GrayscaleWithAlpha });
}
outputFilePathStack.Push(exportPath);
}
SaveToPsdThenLoadAndSaveToPng("grayscale5x5", ColorModes.Cmyk, 16, 5, CompressionMethod.RLE, 0);
SaveToPsdThenLoadAndSaveToPng("argb16bit_5x5", ColorModes.Grayscale, 16, 2, CompressionMethod.RLE, 0);
SaveToPsdThenLoadAndSaveToPng("argb16bit_5x5_no_layers", ColorModes.Grayscale, 16, 2, CompressionMethod.RLE, -1);
SaveToPsdThenLoadAndSaveToPng("argb8bit_5x5", ColorModes.Grayscale, 16, 2, CompressionMethod.RLE, 0);
SaveToPsdThenLoadAndSaveToPng("argb8bit_5x5_no_layers", ColorModes.Grayscale, 16, 2, CompressionMethod.RLE, -1);
SaveToPsdThenLoadAndSaveToPng("cmyk16bit_5x5_no_layers", ColorModes.Grayscale, 16, 2, CompressionMethod.RLE, -1);
SaveToPsdThenLoadAndSaveToPng("index8bit_5x5", ColorModes.Grayscale, 16, 2, CompressionMethod.RLE, -1);

Ảnh Photoshop 16 bit sang chế độ xám 8 bit

Nhưng nếu bạn cần chuyển đổi ảnh Photoshop xám 16 bit sang chế độ xám 8 bit? Bạn cần sử dụng đoạn mã sau. 8 bit là độ sâu bit phổ biến trong các Tệp PSD.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
//The following example demonstrates that reading and saving the Grayscale 16 bit PSD files to 8 bit per channel Grayscale works correctly and without an exception.
string sourceFilePath = "grayscale16bit.psd";
string exportFilePath = "grayscale16bit_Grayscale8_2_RLE.psd";
PsdOptions psdOptions = new PsdOptions()
{
ColorMode = ColorModes.Grayscale,
ChannelBitsCount = 8,
ChannelsCount = 2
};
using (PsdImage image = (PsdImage)Image.Load(sourceFilePath))
{
RasterCachedImage raster = image.Layers[0];
Graphics graphics = new Graphics(raster);
int width = raster.Width;
int height = raster.Height;
Rectangle rect = new Rectangle(width / 3, height / 3, width - (2 * (width / 3)) - 1, height - (2 * (height / 3)) - 1);
graphics.DrawRectangle(new Pen(Color.DarkGray, 1), rect);
image.Save(exportFilePath, psdOptions);
}
string pngExportPath = Path.ChangeExtension(exportFilePath, "png");
using (PsdImage image = (PsdImage)Image.Load(exportFilePath))
{
// Here should be no exception.
image.Save(pngExportPath, new PngOptions() { ColorType = PngColorType.GrayscaleWithAlpha });
}

Chuyển đổi Ảnh xám sang RGB

Trường hợp phức tạp khác nếu bạn cần chuyển đổi Ảnh xám PSD sang Ảnh RGB.

Ảnh xám chỉ có một kênh, nhưng ảnh RGB có 3 kênh: R - đỏ, G - xanh lá, B - xanh dương. Aspose.PSD có thể chuyển đổi từ Ảnh xám sang RGB.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
//The following example demonstrates that reading and saving the Grayscale 16 bit PSD files to 16bit per channel RGB works correctly and without an exception.
string sourceFilePath = "grayscale5x5.psd";
string exportFilePath = "rgb16bit5x5.psd";
PsdOptions psdOptions = new PsdOptions()
{
ColorMode = ColorModes.Rgb,
ChannelBitsCount = 16,
ChannelsCount = 4
};
using (PsdImage image = (PsdImage)Image.Load(sourceFilePath))
{
RasterCachedImage raster = image.Layers[0];
Graphics graphics = new Graphics(raster);
int width = raster.Width;
int height = raster.Height;
Rectangle rect = new Rectangle(width / 3, height / 3, width - (2 * (width / 3)) - 1, height - (2 * (height / 3)) - 1);
graphics.DrawRectangle(new Pen(Color.DarkGray, 1), rect);
image.Save(exportFilePath, psdOptions);
}
string pngExportPath = Path.ChangeExtension(exportFilePath, "png");
using (PsdImage image = (PsdImage)Image.Load(exportFilePath))
{
// Here should be no exception.
image.Save(pngExportPath, new PngOptions() { ColorType = PngColorType.GrayscaleWithAlpha });
}