I have this:
private void CreatePngThumb(Stream str)
{
ISupportedImageFormat Format = new PngFormat();
var S = new Size(100, 100);
using (var OutStream = new MemoryStream())
{
using (var Imf = new ImageFactory(true))
{
Imf.Load(str)
.Resize(S)
.Format(Format)
.BackgroundColor(Color.White)
.Save(OutStream);
}
}
}
str contains a png image, but when i load it (Imf.Load(str)) i get, Input stream is not a supported format.
Why?
Are you absolutely sure that the input stream is correct and hasn't been incorrectly encoded.?
If you look at the list of file signatures here the first four bytes of a png stream in hexadecimal are:
89, 50, 4E, 47
This translates to a decimal representation of:
137, 80, 78, 71
This is true for all png files.
ImageProcessor uses the first four bytes of an image stream to identify the correct format. If the first four bytes are one of the recognised types then the correct ISupportedImageFormat is loaded to process the image. If not, then the error you are seeing is thrown.
Here is the identifier for the PngFormatClass https://github.com/JimBobSquarePants/ImageProcessor/blob/V2/src/ImageProcessor/Imaging/Formats/PngFormat.cs#L37
Closing as the png format is standard and recognised properly by ImageProcessor.
In case this helps someone else, if you are doing some kind of logic to work out if a file is an image that involves reading the file...BEFORE then reading the file into a memorystream for imageprocesser, make sure you reset the file position:
file.InputStream.Position = 0;
Same error but with jpg. Here is the image http://www.uline.com/images/en-US/family2.jpg
Odd. I'm not able to replicate using the code here
I just ran into this today myself with a jpg. Resaving the image in photoshop doesn't seem to help.
ImageProcessor version 2.3.3.0
Using a hex editor, the file is indeed a JPG ffd8.

Continuing to figure it out. The same script is loading images correctly from the same shoot, but a handful are throwing this error.
In case it helps, the exception appears to be thrown at the .load() method in my code:
using (var imageFactory = new ImageFactory(preserveExifData: true))
{
var factory = imageFactory.Load(stream);
/* more code ommitted */
}
Input stream is not a supported format.
ImageProcessor.Common.Exceptions.ImageFormatException: Input stream is not a supported format.
at ImageProcessor.ImageFactory.Load(Stream stream)
@JimBobSquarePants, I can send you a file privately that doesn't work if you'd like.
@kgiszewski Yeah, send me the image please so I can test and inspect it.
I'll DM you a link on Twitter tomorrow when I'm in front of a computer.
Cheers! If you can share a private gist of the relevant bits your code also that would be great.
@JimBobSquarePants, sorry for the delay but I've resolved the issue. You'll be happy (or annoyed) to know that it was fixed by updating my own code.
This code snippet produces intermittent results:
var azureStream = new MemoryStream();
blockBlob.DownloadToStream(azureStream);
using (var imageFactory = new ImageFactory(preserveExifData: true))
{
var factory = imageFactory.Load(azureStream);
Console.WriteLine(factory.CurrentImageFormat.MimeType);
}
Whilst adding a stream position reset makes it work for everything:
var azureStream = new MemoryStream();
blockBlob.DownloadToStream(azureStream);
//adding this fixed it for all
azureStream.Position = 0;
using (var imageFactory = new ImageFactory(preserveExifData: true))
{
var factory = imageFactory.Load(azureStream);
Console.WriteLine(factory.CurrentImageFormat.MimeType);
}
Thanks for all your hard work as usual :+1:
Ah good! No worries, I'll need to have a wee look at the load method to ensure we are resetting the position before checking. I thought it was.
Most helpful comment
In case this helps someone else, if you are doing some kind of logic to work out if a file is an image that involves reading the file...BEFORE then reading the file into a memorystream for imageprocesser, make sure you reset the file position:
file.InputStream.Position = 0;