Welcome Guest! You need to login or register to make posts.

Notification

Icon
Error

Options
Go to last post Go to first unread
bma  
#1 Posted : Monday, May 16, 2016 8:53:10 AM(UTC)
bma

Rank: Member

Groups: Member
Joined: 1/12/2016(UTC)
Posts: 23

Thanks: 8 times
Hello,

I have the following error when I try to convert a PSD file to JPG: "The Frame is Empty"

Please see picture attached.

Code Sample:
Code:
public static void Main(string[] args)
{
	const string path = @"D:\Paper-Bag-packaging-Mockup-psd.psd";
	const string pathOutput = @"D:\teste2.jpg";

	var file = File.ReadAllBytes(path);

	using (var imageStream = new MemoryStream(file))
	using (var exportedImageStream = new MemoryStream())
	using (var imageReader = ImageReader.Create(imageStream))
	using (var bitmap = imageReader.Frames[0].GetBitmap())
	{
		// merge photoshop layers
		if (imageReader.FileFormat == FileFormat.Psd)
		{
			((PsdReader)imageReader).MergeLayers(bitmap);
		}

		bitmap.Save(exportedImageStream, new JpegSettings { Quality = 85 });

		File.WriteAllBytes(pathOutput, exportedImageStream.GetBuffer());
	}

}


Thank you!

Kind Regards

Edited by user Monday, May 16, 2016 8:57:35 AM(UTC)  | Reason: Not specified

File Attachment(s):
Paper-Bag-packaging-Mockup-psd.zip (12,116kb) downloaded 8 time(s).
Fedor  
#2 Posted : Tuesday, May 17, 2016 9:43:15 PM(UTC)
Fedor

Rank: Advanced Member

Groups: Member, Administration, Moderator
Joined: 7/28/2003(UTC)
Posts: 1,660

Thanks: 5 times
Was thanked: 76 time(s) in 74 post(s)
Some PSD frames can contain no prerendered image data, that's why you receive exception when you request a bitmap:

Code:
imageReader.Frames[0].GetBitmap()


Here is the corrected code:

Code:
public static void Main(string[] args)
{
	const string path = @"D:\Paper-Bag-packaging-Mockup-psd.psd";
	const string pathOutput = @"D:\teste2.jpg";

	var file = File.ReadAllBytes(path);

	using (var imageStream = new MemoryStream(file))
	using (var exportedImageStream = new MemoryStream())
	using (var imageReader = ImageReader.Create(imageStream))
	{
		Bitmap bitmap = null;

		try
		{
			// merge photoshop layers
			if (imageReader.FileFormat == FileFormat.Psd)
			{
				bitmap = new Bitmap();
				((PsdReader)imageReader).MergeLayers(bitmap);
			}
			else
			{
				bitmap = imageReader.Frames[0].GetBitmap();
			}

			bitmap.Save(exportedImageStream, new JpegSettings { Quality = 85 });

			File.WriteAllBytes(pathOutput, exportedImageStream.GetBuffer());
		}
		finally
		{
			if (bitmap != null)
				bitmap.Dispose();
		}
	}
}
Best regards,
Fedor Skvortsov
bma  
#3 Posted : Wednesday, May 18, 2016 2:13:47 AM(UTC)
bma

Rank: Member

Groups: Member
Joined: 1/12/2016(UTC)
Posts: 23

Thanks: 8 times
Thank you!
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.