画像ファイルの作成

Aspose.PSD for .NETを使用すると、開発者は独自の画像を作成できます。 Imageクラスによって公開された静的Createメソッドを使用して新しい画像を作成します。必要なのは、出力画像形式のためのImageOptions名前空間のクラスのいずれかの適切なオブジェクトを提供するだけです。 画像ファイルを作成するには、まずImageOptions名前空間のクラスのインスタンスを作成します。これらのクラスは出力画像形式を決定します。 以下はImageOptions名前空間からのいくつかのクラスです(現在はPSDファイル形式ファミリーだけが作成されてサポートされています):

PsdOptions は PSDファイルを作成するためのオプションを設定します。出力パスを設定するか、ストリームを設定することで画像ファイルを作成できます。

パスを設定して作成

ImageOptions 名前空間からPsdOptionsを作成し、さまざまなプロパティを設定します。 設定する最も重要なプロパティはSourceプロパティです。 このプロパティは画像データがどこにあるかを指定します(ファイル内またはストリーム内)。 下の例では、ソースはファイルです。 プロパティを設定した後、Imageクラスのstatic Create メソッドの1つにオブジェクトを渡します。 オブジェクトには幅と高さのパラメータも必要です。 幅と高さはピクセル単位で定義されています。

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
// Creates an instance of PsdOptions and set its various properties
PsdOptions psdOptions = new PsdOptions();
psdOptions.CompressionMethod = CompressionMethod.RLE;
// Define the source property for the instance of PsdOptions. Second boolean parameter determines if the file is temporal or not
psdOptions.Source = new FileCreateSource(desName, false);
// Creates an instance of Image and call Create method by passing the PsdOptions object
using (Image image = Image.Create(psdOptions, 500, 500))
{
image.Save();
}

ストリームを使用して作成

ストリームを使用してイメージを作成するプロセスは、パスを使用する場合と同じです。 違いは、Streamオブジェクトをそのコンストラクタに渡し、Sourceプロパティに割り当てるStreamSourceのインスタンスを作成する必要がある点です。

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string desName = dataDir + "CreatingImageUsingStream_out.bmp";
// Creates an instance of BmpOptions and set its various properties
BmpOptions ImageOptions = new BmpOptions();
ImageOptions.BitsPerPixel = 24;
// Create an instance of System.IO.Stream
Stream stream = new FileStream(dataDir + "sample_out.bmp", FileMode.Create);
// Define the source property for the instance of BmpOptions Second boolean parameter determines if the Stream is disposed once get out of scope
ImageOptions.Source = new StreamSource(stream, true);
// Creates an instance of Image and call Create method by passing the BmpOptions object
using (Image image = Image.Create(ImageOptions, 500, 500))
{
// Do some image processing
image.Save(desName);
}

画像ファイルの開封

開発者は、様々な目的で既存のPSD画像ファイルを開くためにAspose.PSD for .NET APIを使用できます。画像に効果を追加したり、既存のファイルを別の形式に変換するなどの目的に使えます。 Aspose.PSDは既存のファイルを開くための2つの標準方法を提供します:ファイルから、またはストリームから。

ディスクから開く

ファイルのパスとファイル名をImageクラスの staticメソッドLoadにパラメータとして渡すことで、イメージファイルを開きます。

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string sourceFile = dataDir + @"sample.psd";
string destName = dataDir + "result.png";
// load PSD image and replace the non found fonts.
using (Image image = Image.Load(sourceFile))
{
PsdImage psdImage = (PsdImage)image;
psdImage.Save(destName, new PngOptions());
}

ストリームを使用して開く

開く必要があるイメージがストリームとして保存されている場合は、Loadメソッドのオーバーロードバージョンを使用します。 このオーバーロードバージョンは、イメージを開くためにStreamオブジェクトを引数として受け入れます。

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string sourceFile = dataDir + @"sample.psd";
string destName = dataDir + "result.png";
FileStream fStream = new FileStream(sourceFile, FileMode.Open);
fStream.Position = 0;
// load PSD image and replace the non found fonts.
using (Image image = Image.Load(fStream))
{
PsdImage psdImage = (PsdImage)image;
MemoryStream stream = new MemoryStream();
psdImage.Save(stream, new PngOptions());
}

レイヤーとしてイメージをロード

本記事では、Aspose.PSDを使用して画像をレイヤーとしてロードする方法を示します。 Aspose.PSD APIは、この目標を達成するための効率的で使いやすいメソッドを公開しています。 Aspose.PSDは、PsdImageクラスのAddLayerメソッドを使って画像をPSDファイルにレイヤーとして追加します。

PSDに画像をレイヤーとしてロードする手順は以下の通りです:

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string filePath = dataDir + "PsdExample.psd";
string outputFilePath = dataDir + "PsdResult.psd";
using (var image = new PsdImage(200, 200))
{
using (var im = Image.Load(filePath))
{
Layer layer = null;
try
{
layer = new Layer((RasterImage)im);
image.AddLayer(layer);
}
catch (Exception e)
{
if (layer != null)
{
layer.Dispose();
}
throw;
}
}
image.Save(outputFilePath);
}

ストリームからのレイヤーとしてイメージをロード

本記事では、ストリームからイメージをレイヤーとしてロードする方法を示します。 イメージをストリームからロードするには、単純にイメージを含むストリームオブジェクトをLayerクラスのコンストラクタに渡します。 作成したレイヤーをPsdImageクラスが公開するAddLayerメソッドを使って追加し、結果を保存します。

サンプルコードは以下の通りです。

// 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);
}

画像ファイルの保存

Aspose.PSDを使用すると、画像ファイルをゼロから作成することができます。既存の画像ファイルを編集する手段も提供します。 画像が作成されたり変更されたりすると、通常はファイルがディスクに保存されます。 Aspose.PSDには、パスを指定して画像をディスクに保存するためのメソッドやストリームオブジェクトを使用するメソッドが用意されています。

ディスクに保存

Imageクラスは画像オブジェクトを表すため、このクラスは画像を作成、ロード、保存するために必要なすべてのツールを提供します。 画像を保存するには、ImageクラスのSaveメソッドを使用します。 Saveメソッドの1つのオーバーロードバージョンはファイルの場所を文字列として受け取ります。

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string sourceFile = dataDir + @"sample.psd";
string destName = dataDir + "result.png";
// load PSD image and replace the non found fonts.
using (Image image = Image.Load(sourceFile))
{
PsdImage psdImage = (PsdImage)image;
psdImage.Save(destName, new PngOptions());
}

ストリームに保存

Saveメソッドの別のオーバーロードバージョンはStreamオブジェクトを引数として受け入れ、画像ファイルをストリームに保存します。

ImageクラスのコンストラクタでCreateOptionsのいずれかを指定して画像が作成された場合、Imageクラスの初期化時に指定したパスまたはストリームに画像は自動的に保存されます。 保存メソッドを呼び出すときは、パラメータを受け取らないSaveメソッドを使用します。

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string sourceFile = dataDir + @"sample.psd";
string destName = dataDir + "result.png";
// load PSD image and replace the non found fonts.
using (Image image = Image.Load(sourceFile))
{
PsdImage psdImage = (PsdImage)image;
MemoryStream stream = new MemoryStream();
psdImage.Save(stream, new PngOptions());
}

欠落フォントの置き換え設定

開発者は、PSD文書をラスターイメージ(PNG、JPG、BMP形式に)変換して保存する際に、欠落フォントの代替としてデフォルトフォント名を設定するなど、さまざまな目的で既存のPSD画像ファイルをAspose.PSD for .NET APIを使用してロードできます。 画像が変更されたら、ファイルはディスクに保存されます。

// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET
string sourceFileName = "sample_konstanting.psd";
string[] outputs = new string[]
{
"replacedfont0.tiff",
"replacedfont1.png",
"replacedfont2.jpg"
};
using (PsdImage image = (PsdImage)Image.Load(sourceFileName, new PsdLoadOptions()))
{
// This way you can use different fonts for different outputs
image.Save(outputs[0], new TiffOptions(TiffExpectedFormat.TiffJpegRgb) { DefaultReplacementFont = "Arial" });
image.Save(outputs[1], new PngOptions { DefaultReplacementFont = "Verdana" });
image.Save(outputs[2], new JpegOptions { DefaultReplacementFont = "Times New Roman" });
}