PSD Convert between different Color Modes
You can learn supported color modes of Aspose.PSD from this article.
Aspose.PSD, for example, supports conversion from 8 to 16 bit per pixel and vice versa. CMYK ICC-Profile conversions and other conversation from one type bit depth to other one. Here you can see examples of how to convert to Grayscale in PSD File.
Convert from CMYK, RGB, Grayscale to Grayscale Color Mode
Below provided sample code demonstrates how to make CMYK, RGB, or Grayscale conversion without 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); |
16-bit photoshop image to 8-bit grayscale mode
But what if you need to convert 16-bit Grayscale Photoshop image to 8-bit grayscale mode? You need to use the following code snippet. 8 bits is a common bit depth in PSD Files.
// 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 }); | |
} |
Convert Grayscale to RGB
The other complex case if you need to convert Grayscale PSD Image to RGB Image.
Grayscale images have only one channel, but RGB images have 3 channels: R - red, G - green, B - blue. Aspose.PSD can convert Grayscale to 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 }); | |
} |