Detect Load Format Issue

Hello,
I opened a CR2 image in Photoshop 2023 and save it as PSD, then passed this PSD file to Imaging.Image.GetFileFormat(InputStream) I will get Unknown!

Since there were many many similar issues in other Aspose products with file format detections which I’ve reported, wanted to share a friendly tip, for some formats, magic numbers are pretty good and safe to evaluate!
For Psd, only the first 4 bytes of file is needed, very fast, almost instant detection
Byte.Take(4).SequenceEqual({&H38, &H42, &H50, &H53})

Thanks.

Update: Tested many PSD files, always Unknown is returning!

Hello, @australian.dev.nerds.
Would you pay attention to the supported format list?
Aspose.Imaging can only export to PSD format, but can’t load it.

If need to load PSD image you can use Aspose.PSD library.

1 Like

Hello,
I’ve got objection here:

Imaging.Image.GetFileFormat(InputStream) result has a PSD enum, not supporting Psd for load is just fine, but existance of Psd in the enum list and returning Unknown is odd, what about just adding the Psd detection to Imaging.Image.GetFileFormat?
It’s as easy as abc, just check the following 4 starting bytes:
Byte.Take(4).SequenceEqual({&H38, &H42, &H50, &H53})
Thanks for your consideration :slight_smile:

Hi,
Image.GetFileFormat returns the correct format only if Aspose.Imaging supports this format for loading/creating. Otherwise, it returns Unknown.
We retain the enum value FileFormat.Psd only for the back compatibility. A few years ago Aspose.Imaging supported loading/saving PSD format, but now there is ASpose.PSD for that.
We do not have a purpose for implementing detection for all image formats.

1 Like

Hello and thanks so much for your kind help and great info provided.

Just if I use Aspose PSD for loading PSD and wanna save it as Apng or Dicom, I’ve been told to use LoadArgb32Pixels/SaveArgb32Pixels for this task.

Because Aspose PSD does not support saving as Apng or Dicom.

However, all metadata will be lost during this process if I use this method.
Do you have any recommendation to workaround this?
Best.

@australian.dev.nerds, you can ask this question in psd related forum https://forum.aspose.com/c/psd/34, maybe they can advice you something.

@australian.dev.nerds
If I understood you correctly, you can use Aspose.PSD for loading and then Aspose.Imaging for saving to APNG, Dicom, etc.
You may write something like this

using Aspose.Imaging;

Aspose.PSD.Rectangle rect; 
int[] pixels;

using (var psdImage = (Aspose.PSD.FileFormats.Psd.PsdImage)Aspose.PSD.Image.Load("somefile.psd"))
{
	rect = psdImage.getBounds();
	pixels = psdImage.LoadArgb32Pixels(rect));
	// you might also take here a metadata, profiles and so on.
}

var exportOptions = new DicomOptions();
exportOptions.Source = new Sources.FileCreateSource("D:\\Temp\\output.dicom", False);
using (var outImage = (RasterImage)Image.Create(rect.Width, rect.Height, exportOptions))
{
	// here you might also copy to outImage the metadata, profiles and so on taken from PSD image above.
	outImage.SaveArgb32Pixels(new Rectangle(0, 0, rect.Width, rect.Height), pixels);
	outImage.Save(); // save to D:\Temp\output.dicom
	outImage.Save("any_other.apng", new ApngOptions()); // or export to other formats.
}