Adobe Photoshopフォーマットの操作

PSDレイヤーを他のレイヤーに統合

PSDファイルへの画像エクスポート

PSD、PhotoShop Documentは、Adobe Photoshopが画像を扱うために使用するデフォルトのファイル形式です。Aspose.PSDを使用すると、PSDファイルにファイルをロード、編集、保存して、Photoshopで開いて編集できるようにすることができます。この記事では、Aspose.PSDを使用してファイルをPSD形式で保存する方法と、保存する際に使用できる設定の一部について説明します。PsdOptionsは、PSDに画像をエクスポートするために使用されるImageOptions名前空間の特殊なクラスです。PSDにエクスポートするには、既存の画像ファイル(例:サムネイル)から読み込まれたImageクラスのインスタンスを作成するか、ゼロから作成します。この記事では、以下の例で画像がゼロから作成されます。作成され、ピクセルデータが代入された画像は、ImageクラスのSaveメソッドを使用して保存し、2番目の引数としてPsdOptionsオブジェクトを指定します。PsdOptionsクラスのいくつかのプロパティを設定して高度な変換が行えます。いくつかのプロパティはColorMode、CompressionMethod、Versionです。Aspose.PSDは、CompressionMethod列挙型を介して以下の圧縮メソッドをサポートしています:

  • CompressionMethod.Raw
  • CompressionMethod.RLE
  • CompressionMethod.ZipWithoutProtection
  • CompressionMethod.ZipWithProtection

以下のカラーモードは、ColorModes列挙型を介してサポートされています:

  • ColorModes.Bitmap
  • ColorModes.Grayscale
  • ColorModes.RGB

PSD v4.0、v5.0およびそれ以上のサムネイルリソース、またはPSD v4.0およびそれ以上のグリッドやガイドリソースなど、追加のリソースを追加することができます。以下のコードは、ゼロからImageファイルを作成し、ピクセルを埋め、RLE圧縮およびグレースケールカラーモードでPSDに保存します。以下のコードスニペットでは、画像をPSDにエクスポートする方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
// Create a new image from scratch.
using (PsdImage image = new PsdImage(300, 300))
{
// Fill image data.
Graphics graphics = new Graphics(image);
graphics.Clear(Color.White);
var pen = new Pen(Color.Brown);
graphics.DrawRectangle(pen, image.Bounds);
// Create an instance of PsdOptions, Set it's various properties Save image to disk in PSD format
PsdOptions psdOptions = new PsdOptions();
psdOptions.ColorMode = ColorModes.Rgb;
psdOptions.CompressionMethod = CompressionMethod.Raw;
psdOptions.Version = 4;
image.Save(dataDir + "ExportImageToPSD_output.psd", psdOptions);
}

PSDレイヤーに画像をインポート

この記事では、Aspose.PSDを使用してPSDレイヤーに画像を追加またはインポートする方法を示しています。Aspose.PSDのAPIには、この目標を達成するための効率的で使いやすいメソッドが公開されています。Aspose.PSDは、LayerクラスのDrawImageメソッドを使用してPSDファイルに画像を追加/インポートする機能を公開しています。DrawImageメソッドには、PSDファイルに画像を追加/インポートするための場所と画像の値が必要です。PSDレイヤーに画像をインポートする手順は以下のとおりです:

  • Imageクラスによって公開されるLoadファクトリメソッドを使用して画像としてPSDファイルをロードします。
  • Png、Jpeg、Tiff、Gif、Bmp、Psdまたはj2kファイルを使用してレイヤークラスのインスタンスを作成します。
  • Layerクラスを使用してLayerをPsdに追加します。
  • 結果を保存します。

以下のコードスニペットは、画像をPSDレイヤーにインポートする方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string outputFilePath = dataDir + "PsdResult.psd";
var filesList = new string[]
{
"PsdExample.psd",
"BmpExample.bmp",
"GifExample.gif",
"Jpeg2000Example.jpf",
"JpegExample.jpg",
"PngExample.png",
"TiffExample.tif",
};
using (var image = new PsdImage(200, 200))
{
foreach (var fileName in filesList)
{
string filePath = dataDir + fileName;
using (var stream = new FileStream(filePath, FileMode.Open))
{
Layer layer = null;
try
{
layer = new Layer(stream);
image.AddLayer(layer);
}
catch (Exception e)
{
if (layer != null)
{
layer.Dispose();
}
throw e;
}
}
}
image.Save(outputFilePath);
}

PSDレイヤーでの色の置き換え

この記事では、Aspose.PSDを使用してPSDレイヤーに画像を追加またはインポートする方法を示しています。Aspose.PSDのAPIには、この目標を達成するための効率的で使いやすいメソッドが公開されています。Aspose.PSDは、レイヤークラスのDrawImageメソッドを使用してPSDファイルに画像を追加/インポートする機能を公開しています。DrawImageメソッドには、画像が追加されるPSDファイルでの場所と画像インスタンスが指定されます。PSDレイヤーに画像をインポートする手順は以下のとおりです:

  • Imageクラスによって公開されるLoadファクトリメソッドを使用して画像としてPSDファイルをロードします。
  • レイヤークラスのインスタンスを作成し、PSDイメージレイヤーをそれに割り当てます。
  • 追加する画像をロードするか、ゼロから作成します。
  • 位置と画像インスタンスを指定してLayer.DrawImageメソッドを呼び出します。
  • 結果を保存します。

以下のコードスニペットは、PSDレイヤーに画像をインポートする方法を示しています:

// 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 caste it into PsdImage
using (PsdImage image = (PsdImage)Image.Load(dataDir + "sample.psd"))
{
foreach (var layer in image.Layers)
{
if (layer.Name == "Rectangle 1")
{
layer.HasBackgroundColor = true;
layer.BackgroundColor = Color.Orange;
}
}
image.Save(dataDir + "asposeImage02.psd");
}

PSDファイルからサムネイルを作成

PSDはAdobeのPhotoshopアプリケーションの固有のドキュメント形式です。Adobe Photoshop(バージョン5.0以降)は、プレビュー表示用のサムネイル情報を画像リソースブロックに保存します。このリソースには、初期の28バイトのヘッダーと、RGB(赤、緑、青)の順でJFIFサムネイルが含まれます。Aspose.PSD APIは、PSDファイルのリソースにアクセスするための使いやすいメカニズムを提供しています。これらのリソースにはサムネイルリソースも含まれ、アプリケーションのニーズに応じて取得してディスクに保存することができます。以下のコードスニペットは、PSDファイルからサムネイルを作成する方法を示しています:

// 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 caste it into PsdImage
using (PsdImage image = (PsdImage)Image.Load(dataDir + "sample.psd"))
{
int index = 0;
// Iterate over the PSD resources
foreach (var resource in image.ImageResources)
{
index++;
// Check if the resource is of thumbnail type
if (resource is ThumbnailResource)
{
// Retrieve the ThumbnailResource and Check the format of the ThumbnailResource
var thumbnail = (ThumbnailResource)resource;
if (thumbnail.Format == ThumbnailFormat.KJpegRgb)
{
// Create a new image by specifying the width and height, Store the pixels of thumbnail on to the newly created image and save image
PsdImage thumnailImage = new PsdImage(thumbnail.Width, thumbnail.Height);
thumnailImage.SavePixels(thumnailImage.Bounds, thumbnail.ThumbnailData);
thumnailImage.Save(dataDir + "CreateThumbnailsFromPSDFiles_out_" + index.ToString() + ".bmp", new BmpOptions());
}
}
}
}

インデックス化PSDファイルの作成

Aspose.PSD for .NET APIを使用して、ゼロからインデックス化されたPSDファイルを作成できます。この記事では、PsdOptionsおよびPsdImageクラスを使用して、新しく作成されたキャンバスに図形を描画しながらインデックス化されたPSDを作成する方法を示しています。インデックス化されたPSDファイルを作成するには、以下の簡単な手順が必要です:

  • PsdOptionsのインスタンスを作成し、そのソースを設定します。
  • PsdOptionsのColorModeプロパティをColorModes.Indexedに設定します。
  • RGBスペースからカラーパレットを作成し、PsdOptionsのPaletteプロパティとして設定します。
  • CompressionMethodプロパティを必要な圧縮アルゴリズムに設定します。
  • PsdImage.Createメソッドを呼び出して新しいPSDイメージを作成します。
  • グラフィックスを描画したり、他の操作を行います。
  • すべての変更を確定するためにPsdImage.Saveメソッドを呼び出します。

以下のコードスニペットは、インデックス化されたPSDファイルの作成方法を示しています:

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
// Create an instance of PsdOptions and set it's properties
var createOptions = new PsdOptions();
createOptions.Source = new FileCreateSource(dataDir + "Newsample_out.psd", false);
createOptions.ColorMode = ColorModes.Indexed;
createOptions.Version = 5;
// Create a new color palette having RGB colors, Set Palette property & compression method
Color[] palette = { Color.Red, Color.Green, Color.Blue, Color.Yellow };
createOptions.Palette = new PsdColorPalette(palette);
createOptions.CompressionMethod = CompressionMethod.RLE;
// Create a new PSD with PsdOptions created previously
using (var psd = Image.Create(createOptions, 500, 500))
{
// Draw some graphics over the newly created PSD
var graphics = new Graphics(psd);
graphics.Clear(Color.White);
graphics.DrawEllipse(new Pen(Color.Red, 6), new Rectangle(0, 0, 400, 400));
psd.Save();
}

PSDレイヤーをラスター画像にエクスポート

Aspose.PSD for .NETを使用すると、PSDファイル内のレイヤーをラスター画像にエクスポートできます。レイヤーを画像にエクスポートするには、Aspose.PSD.FileFormats.Psd.Layers.Layer.Saveメソッドを使用してレイヤーを画像にエクスポートします。次のサンプルコードは、PSDファイルをロードし、そのレイヤーをAspose.PSD.FileFormats.Psd.Layers.Layer.Saveを使用してPNG画像にエクスポートする方法を示しています。すべてのレイヤーがPNG画像にエクスポートされたら、お好みの画像ビューアを使用してそれらを開くことができます。以下のコードスニペットは、PSDレイヤーをラスター画像としてエクスポートする方法を示しています:

// 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 caste it into PsdImage
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "sample.psd"))
{
// Create an instance of PngOptions class
var pngOptions = new PngOptions();
pngOptions.ColorType = PngColorType.TruecolorWithAlpha;
// Loop through the list of layers
for (int i = 0; i < psdImage.Layers.Length; i++)
{
// Convert and save the layer to PNG file format.
psdImage.Layers[i].Save(string.Format("layer_out{0}.png", i + 1), pngOptions);
}
}

PSDファイルのテキストレイヤーの更新

Aspose.PSD for .NETを使用すると、PSDファイルのテキストレイヤー内のテキストを操作できます。PSDレイヤー内のテキストを更新するには、Aspose.PSD.FileFormats.Psd.Layers.TextLayerクラスを使用してPSDレイヤーのテキストを更新し、Aspose.PSD.FileFormats.Psd.Layers.TextLayer.UpdateTextメソッドを使用して新しい名前でPSDファイルを保存します。

// 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 + "layers.psd"))
{
foreach (var layer in psdImage.Layers)
{
if (layer is TextLayer)
{
TextLayer textLayer = layer as TextLayer;
textLayer.UpdateText("test update", new Point(0, 0), 15.0f, Color.Purple);
}
}
psdImage.Save(dataDir + "UpdateTextLayerInPSDFile_out.psd");
}

フラット化されたPSDを検出

Aspose.PSD for .NETを使用すると、特定のPSDファイルがフラット化されているかどうかを検出できます。Aspose.PSD.FileFormats.Psd.PsdImageクラスのIsFlattenプロパティがこの機能を実現するために導入されました。

// 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 + "layers.psd"))
{
// Do processing, Get the true value if PSD is flatten and false in case the PSD is not flatten.
Console.WriteLine(psdImage.IsFlatten);
}

PSDレイヤーをマージ

This article shows how to merge layers in a PSD file while converting a PSD file to JPG with Aspose.PSD. In the example below, an existing PSD file is loaded by passing the file path to the Image class static Load method. Once it is loaded, convert/cast the image to PsdImage. Create an instance of the JpegOptions class and set it various properties. Now call the Save method of the PsdImage instance. The following code snippet shows you how to merge PSD layers.

// 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 + "layers.psd"))
{
// Create JPEG option class object, Set its properties and save image
var jpgOptions = new JpegOptions();
psdImage.Save(dataDir + "MergePSDlayers_output.jpg", jpgOptions);
}

PSDファイルでのGrayscale with Alphaのサポート

This article shows how to support Grayscale with Alpha for PSD file while converting a PSD file to PNG with Aspose.PSD. In the example below, an existing PSD file is loaded by passing the file path to the Image class static Load method. Once it is loaded, convert/cast the image to PsdImage. Create an instance of the PngOptions class and set its various properties. Set the ColorType property to TruecolorWithAlpha. Now call the Save method of the PngOptions instance. The following code snippet shows you how to convert to Grayscale PNG with Alpha.

PSDレイヤー効果のサポート

This article shows how to support different effects like Blur, Inner Glow, Outer Glow for PSD layer. The following code snippet shows you how to support layer effects.

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string sourceFileName = dataDir + "layers.psd";
string output = dataDir + "layers.png";
using (PsdImage image = (PsdImage)Image.Load(sourceFileName,
new ImageLoadOptions.PsdLoadOptions()
{
LoadEffectsResource = true,
UseDiskForLoadEffectsResource = true
}))
{
Debug.Assert(image.Layers[2] != null, "Layer with effects resource was not recognized");
image.Save(output, new ImageOptions.PngOptions()
{
ColorType =
FileFormats.Png
.PngColorType
.TruecolorWithAlpha
});
}