PNG画像の操作
PNG画像の透明度の指定
PNG形式で画像を保存する利点の1つは、PNGに透明な背景を持たせることができる点です。Aspose.PSD for .NETは、以下のセクションで示されているように、PNG画像およびラスター画像の透明度を指定する機能を提供しています。Aspose.PSD for .NET APIは、新しいPNG画像を作成する際や既存の画像をPNG形式に変換する際に、透明色を指定するために使用できます。そのために、Aspose.PSD for .NET APIは、TransparentColorプロパティとPngColorType列挙型を公開しており、PNG画像内で透明にする任意の色を指定するために設定できます。以下の提供されたコードスニペットは、指定された色を透明にして既存のPSD画像をPNG画像に変換する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "sample.psd")) | |
{ | |
// specify the PNG image transparency options and save to file. | |
psdImage.TransparentColor = Color.White; | |
psdImage.HasTransparentColor = true; | |
PngOptions opt = new PngOptions(); | |
psdImage.Save(dataDir + "Specify_Transparency_result.png", new PngOptions()); | |
} |
PNG画像の解像度の設定
Aspose.PSD for .NETは、すべての画像形式(PNGを含む)の解像度を設定するために使用できるResolutionSettingクラスを公開しています。この記事では、Aspose.PSD for .NET APIを使用してPNG画像形式の水平および垂直解像度パラメータを設定する方法を示しています。以下のコードスニペットでは、既存のPSD画像を読み込んでPNG形式に変換し、また解像度を変更します。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "sample.psd")) | |
{ | |
// Create an instance of PngOptions, Set the horizontal & vertical resolutions and Save the result on disc | |
PngOptions options = new PngOptions(); | |
options.ResolutionSettings = new ResolutionSetting(72, 96); | |
psdImage.Save(dataDir + "SettingResolution_output.png", options); | |
} |
PNGファイルの圧縮
Portable Network Graphic(PNG)は、ネットワークを介してビットマップを転送するための無損失圧縮形式です。任意のプログラムで画像をPNGファイルとして保存するとき、0から最大レベルまでの範囲で圧縮レベルを選択するように求められることがあります。この値を設定することで、ファイルサイズが圧縮され、画質が低下することはありません。この記事では、Aspose.PSD APIがPNGファイルサイズを制御する方法について説明します。Aspose.PSD APIsを使用して、PngOptionsクラスを介してPNGファイル形式の圧縮レベルを設定できます。このクラスには、int型のCompressionLevelプロパティがあり、このプロパティは0から9までの値を受け入れます(9が最大圧縮です)。以下の提供されたコードスニペットは、Aspose.PSD for .NET APIを使用して圧縮レベルを設定する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "sample.psd")) | |
{ | |
// Loop over possible CompressionLevel range | |
for (int i = 0; i <= 9; i++) | |
{ | |
// Create an instance of PngOptions for each resultant PNG, Set CompressionLevel and Save result on disk | |
PngOptions options = new PngOptions(); | |
options.CompressionLevel = i; | |
psdImage.Save(dataDir + i + "_out.png", options); | |
} | |
} |
PNG画像のビット深度の指定
画像のビット深度とは、ビットマップ画像内の単一のピクセルの色を示すのに使用されるビット数です。他のすべてのビットマップ形式と同様に、PNGカラーの深度も1ビット(2色)、2ビット(4色)、4ビット(16色)、8ビット(256色)などのようにビットで表されます。Aspose.PSD for .NET APIは、PngOptionsクラスによって公開されるBitDepthプロパティを使用してPNG画像のビット深度を設定することができます。現時点では、BitDepthプロパティは、グレースケールおよびインデックスカラータイプの場合に1、2、4、または8ビットに設定できます。ほかのすべてのカラータイプでは、8ビットのみがサポートされます。以下の提供されたコードスニペットは、PNG画像のビット深度を設定する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "sample.psd")) | |
{ | |
// Create an instance of PngOptions, Set the desired ColorType, BitDepth according to the specified ColorType and save image | |
PngOptions options = new PngOptions(); | |
options.ColorType = PngColorType.Grayscale; | |
options.BitDepth = 1; | |
psdImage.Save(dataDir + "SpecifyBitDepth_out.png", options); | |
} |
PNG画像にフィルター効果を適用する
画像のビット深度とは、ビットマップ画像内の単一のピクセルの色を示すのに使用されるビット数です。他のすべてのビットマップ形式と同様に、PNGカラーの深度も1ビット(2色)、2ビット(4色)、4ビット(16色)、8ビット(256色)などのようにビットで表されます。Aspose.PSD for .NET APIは、PngOptionsクラスによって公開されるBitDepthプロパティを使用してPNG画像のビット深度を設定することができます。現時点では、BitDepthプロパティはグレースケールおよびインデックスカラータイプの場合に1、2、4、または8ビットに設定できます。ほかのすべてのカラータイプでは、8ビットのみがサポートされます。以下の提供されたコードスニペットは、PNG画像のビット深度を設定する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "sample.psd")) | |
{ | |
// Create an instance of PngOptions, Set the PNG filter method and Save changes to the disc | |
PngOptions options = new PngOptions(); | |
options.FilterType = PngFilterType.Paeth; | |
psdImage.Save(dataDir + "ApplyFilterMethod_out.png", options); | |
} |
透明なPNG画像の背景色の変更
PNG形式の画像は透明な背景を持つことができます。Aspose.PSD for .NETは、透明な背景を持つPNG画像の背景色を変更する機能を提供しています。Aspose.PSD for .NET APIは、透明なPNG画像の背景色を設定/変更するために使用できます。以下の提供されたコードスニペットは、透明なPNG画像の背景色を設定/変更する方法を示しています。
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "sample.psd")) | |
{ | |
int[] pixels = psdImage.LoadArgb32Pixels(psdImage.Bounds); | |
// Iterate through the pixel array and Check the pixel information | |
//that if it is a transparent color pixel and Change the pixel color to white | |
int transparent = psdImage.TransparentColor.ToArgb(); | |
int replacementColor = Color.Yellow.ToArgb(); | |
for (int i = 0; i < pixels.Length; i++) | |
{ | |
if (pixels[i] == transparent) | |
{ | |
pixels[i] = replacementColor; | |
} | |
} | |
// Replace the pixel array into the image. | |
psdImage.SaveArgb32Pixels(psdImage.Bounds, pixels); | |
psdImage.Save(dataDir + "ChangeBackground_out.png", new PngOptions()); | |
} |