Konwertowanie PSD między różnymi trybami kolorystycznymi
Możesz dowiedzieć się, które tryby kolorystyczne obsługuje Aspose.PSD z tego artykułu.
Aspose.PSD obsługuje konwersję na przykład z 8 na 16 bitów na piksel i odwrotnie. Konwersje profili ICC CMYK oraz inne konwersje z jednego rodzaju głębokości bitowej na drugą. Tutaj można zobaczyć przykłady konwersji na odcienie szarości w plikach PSD.
Konwertuj z trybu kolorystycznego CMYK, RGB, odcienie szarości do trybu kolorystycznego Grayscale
Poniższy przykładowy kod demonstruje, jak dokonać konwersji CMYK, RGB lub odcieni szarości bez użycia Photoshopa.
// 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); |
Obraz 16-bitowy z Photoshopa w trybie odcieni szarości 8-bitowych
Co jednak, jeśli potrzebujesz przekonwertować obraz odcieni szarości 16-bitowy z Photoshopa na tryb odcieni szarości 8-bitowy? Musisz użyć poniższego fragmentu kodu. 8 bitów to powszechna głębia bitowa w plikach 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 }); | |
} |
Konwertuj z odcieni szarości na RGB
Innym złożonym przypadkiem jest konieczność przekonwertowania obrazu PSD odcieni szarości na obraz RGB.
Obrazy odcieni szarości mają tylko jeden kanał, ale obrazy RGB mają 3 kanały: R - czerwony, G - zielony, B - niebieski. Aspose.PSD może przekonwertować odcienie szarości 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 }); | |
} |