PSD Převod mezi různými módy barev

Můžete se dozvědět podporované barevné módy Aspose.PSD z tohoto článku.

Aspose.PSD podporuje například konverzi z 8 na 16 bitů na pixel a naopak. Konverze profilů ICC CMYK a další konverze z jednoho typu bitové hloubky na jiný. Zde můžete vidět příklady, jak převést na stupně šedi v souboru PSD.

Převést z módu barev CMYK, RGB, Grayscale na mód barev Grayscale

Níže uvedený ukázkový kód ukazuje, jak provést konverzi CMYK, RGB nebo stupňů šedi bez Photoshopu.

// 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 bitový obraz photoshopu do režimu 8 bitů stupně šedi

Co když však potřebujete převést 16bitový obraz Photoshopu do režimu 8 bitů stupně šedi? Musíte použít následující kousek kódu. 8 bitů je běžná bitová hloubka v souborech 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 });
}

Převést Grayscale na RGB

Další složitý případ je, pokud potřebujete převést obraz Grayscale PSD na obraz RGB.

Stupňovité obrázky mají pouze jednou kanál, ale obrázky RGB mají 3 kanály: R - červená, G - zelená, B - modrá. Aspose.PSD může převést Grayscale na 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 });
}