המרת PSD בין מצבי צבע שונים

ניתן ללמוד על מצבי הצבעים שנתמכים של Aspose.PSD מתוך מאמר זה.

Aspose.PSD, למשל, תומך בהמרה מ-8 ל-16 ביט לפיקסל ולהיפך. המרות פרופיל ICC של CMYK ושיחות אחרות מסוג אחד של עומק ביט לסוג אחר. כאן ניתן לראות דוגמאות לאיך להמיר לגרייסקייל בקובץ PSD.

להמיר מ-CMYK, RGB, גרייסקייל למצב צבע גרייסקייל

קוד דוגמה המוצף להלן מדגים כיצד לבצע המרה מ-CMYK, 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 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);

תמונת פוטושופ 16 ביט למצב גרייסקייל 8 ביט

אבל מה אם תרצה להמיר תמונת פוטושופ בגרייסקייל ב־16 ביט למצב גרייסקייל ב־8 ביט? עליך להשתמש בקטע הקוד הבא. 8 ביטים הם עומק ביט נפוץ בקבצי 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 });
}

להמיר גרייסקייל ל-RGB

המקרה המורכב השני אם תרצה להמיר תמונת PSD בגרייסקייל לתמונת RGB.

התמונות בגרייסקייל מכילות רק ערוץ אחד, אך תמונות RGB מכילות 3 ערוצים: R - אדום, G - ירוק, B - כחול. Aspose.PSD יכולה להמיר גרייסקייל ל-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 });
}