การสร้าง การเปิด และบันทึกภาพ

สร้างไฟล์ภาพ

Aspose.PSD for .NET ช่วยให้นักพัฒนาสามารถสร้างภาพของตนเองได้ ใช้เมธอดสถิต Create ที่ถูกเปิดเผยโดยคลาส Image เพื่อสร้างภาพใหม่ ทุกอย่างที่คุณต้องการทำก็คือให้วัตถุที่เหมาะสมของหนึ่งในคลาสจากพื้นที่ชื่อ ImageOptions สำหรับรูปแบบภาพเอาท์พุตที่ต้องการ ในการสร้างไฟล์ภาพ ก่อนอื่นให้สร้างอินสแตนซ์ของหนึ่งในคลาสจาก ImageOptions แล้วนำมาใช้งาน คลาสเหล่านี้กำหนดรูปแบบไฟล์ภาพเอาท์พุต ด้านล่างนี้แสดงคลาสบางประเภทจาก ImageOptions (โปรดทราบว่าในปัจจุบันรอครอบของไฟล์ PSD เทคนอความรองรับเฉพาะ):

PsdOptions ตั้งค่าตัวเลือกสำหรับการสร้างไฟล์ PSD ไฟล์ภาพสามารถสร้างโดยการตั้งค่าเส้นทางการออกพอร์ตหรือการตั้งค่าสตรีม

การสร้างโดยการตั้งค่าเส้นทาง

สร้าง PsdOptions จาก ImageOptions แล้วตั้งค่าคุณสมบัติต่าง ๆ คุณสมบัติที่สําคัญสุดคือคุณสมบัติที่ตั้ง นี่คือแหล่งที่ข้อมูลภาพเกิดขึ้น (ในไฟล์หรือสตรีม) ในตัวอย่างด้านล่าง แหล่งที่ต้นฉบับเป็นไฟล์ หลังจากตั้งค่าคุณสมบัติภาพ ถ่ายโอบเจคไปยังหนึ่งในวิธีการสร้างแบบเอาท์พุทแล้วใส่พารามิเตอร์ความกว้างและความสูงเป็นพิกเซล

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

การสร้างโดยใช้สตรีม

ขั้นตอนสําหรับการสร้างภาพโดยใช้สตรีมคล้ายกับการใช้เส้นทาง ความแตกต่างเพียงส่วนที่คุณต้องสร้างอินสแตนซ์ของ StreamSource โดยส่งออบเจค Stream ไปยังคอนสตรักเตอร์ของมันและกำหนดให้กับคุณสมบัติเป็นแหล่ง

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

เปิดไฟล์ภาพ

นักพัฒนาสามารถใช้ Aspose.PSD for .NET API เพื่อเปิดไฟล์ภาพ PSD ที่มีอยู่เพื่อวัตถุประสงค์ต่าง ๆ เช่น การเพิ่มเอฟเฟคที่ไฟล์ภาพหรือเพื่อแปลงไฟล์ที่มีอยู่เป็นรูปแบบอื่น ให้เท่าที่จะทำ นั้น Aspose.PSD จะให้ทางเลือกสองวิธีมาตรฐานในการเปิดไฟล์ที่มีอยู่: มาจากไฟล์หรือจากสตรีม

การเปิดจากดิสก์

เปิดไฟล์ภาพโดยการส่งเส้นทางและชื่อไฟล์เป็นพารามิเตอร์ไปยังเมธอด Load ที่เปิดเผยโดยคลาส Image

// 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 ที่ยึดอินพุทสตรีมอ็อบเจกระตุไปยังคลาสเพื่อเปิดภาพ

// 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 APIs ได้เปิดเผยวิธีการใช้ที่มีประสิทธิภาพและง่ายต่อการใช้เพื่อให้บรรลุวัตถุประสงค์ Aspose.PSD ได้เปิดเผยเมทอด AddLayer ของคลาส PsdImages เพื่อเพิ่มภาพเข้าไปในไฟล์ PSD ให้เป็นเลเยอร์

ขั้นตอนในการโหลดภาพเข้าไปใน PSD เป็นเลเยอร์ง่ายดินดั่งนี้:

  • สร้างอินสแตนซ์ของภาพโดยใช้คลาส PsdImages พร้อมความกว้างและความสูงที่ระบุ
  • โหลดไฟล์ PSD เป็นภาพโดยใช้เมธอดโรงงาน Load ที่เปิดเผยโดยคลาสภาพ
  • สร้างอินสแตนซ์ของคลาสเลเยอร์และมอบภาพ PSD เลเยอร์ไปยังมัน
  • เพิ่มเลเยอร์ที่สร้างขึ้นโดยใช้เมธอด AddLayer ที่เปิดเผยโดยคลาส PsdImages
  • บันทึกผลลัพธ์
// 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);
}

โหลดภาพเป็นเลเยอร์จากสตรีม

บทความนี้สาธารณะใช้ Aspose.PSD เพื่อโหลดภาพเป็นเลเยอร์จากหลุ่มสตรีม การโหลดภาพจากสตรีมเรียบง่ายเพียงส่งออบเจ็คสตรีมที่เก็บภาพไปยังคอนสตรัคเตอร์ของเลเยอร์ และเพิ่มเลเยอร์ที่สร้างขึ้นโดยใช้เมธอด AddLayer ที่เปิดเผยโดยคลาส PsdImages แล้วบันทึกผลลัพธ์

นี่คือรหัสตัวอย่าง

// 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 ช่วยให้คุณสร้างไฟล์ภาพใหม่ตั้งแต่ต้น นอกจากนี้ยังให้วิธีการแก้ไขไฟล์ภาพที่มีอยู่ เมื่อได้สร้างหรือแก้ไขภาพแล้ว ไฟล์จะถูกบันทึกลงดิสก์ เอสพอส.PSD จะให้คุณด้วยเมธอดสำหรับบันทึกรูปภาพไปยังดิสก์โดยระบุเส้นทางหรือใช้ออบเจคสตรีม

บันทึกลงดิสก์

คลาส Images แทนวัตถุภาพ ดังนั้นคลาสนี้ให้เครื่องมือทั้งหมดที่จำเป็นในการสร้าง โหลด และบันทึกไฟล์ภาพ ใช้เมธอดบันทึกภาพ Save บางเวอร์ชันของเมธอด 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;
psdImage.Save(destName, new PngOptions());
}

บันทึกไปยังสตรีม

เวอร์ชันอื่นของเมธอด Save ยอมรับออบเจคสตรีมเป็นอาร์กิวเมนตะแล้วบันทึกไฟล์ภาพไปยังสตรีม

หากภาพถูกสร้างโดยระบุ CreateOptions ใด ๆ ในคอนสตรัคเตอร์ของ Images ภาพจะถูกบันทึกโดยอัตโนมัติไปที่เส้นทางหรือสตรีมที่ให้ระหว่างเริ่มต้น Images จากการเรียกเมธอด 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());
}

การตั้งค่าสำหรับการแทนที่ฟอนต์ที่หายไป

นักพัฒนาสามารถใช้ Aspose.PSD for .NET API เพื่อโหลดไฟล์ภาพ PSD ที่มีอยู่เพื่อวัตถุประสงค์ต่าง ๆ เช่น ตั้งชื่อฟอนต์เริ่มต้นเมื่อบันทึกเอกสาร PSD ให้เป็นภาพเรสเตอร์ (เป็น PNG, JPG และ BMP แบบ) ฟอนต์เริ่มต้นนี้ควรใช้เป็นการแทนที่สำหรับฟอนต์ที่หายไปทั้งหมด (ฟอนต์ที่ไม่พบในระบบปฏิบัติการปัจจุบัน) เมื่อภาพถูกปรับเปลี่ยนผลลัพธ์จะถูกบันทึกลงดิสก์

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