Imagesharp: Getting "Image cannot be loaded. Available decoders: Jpeg, Png, Bmp, Gif"

Created on 4 May 2019  路  3Comments  路  Source: SixLabors/ImageSharp

Last version of ImageSharp running

Hi.

I'm trying to understand why this is happening and went through a lot of other issues with this symptom, but can't solve this. I already used a lot of different random images.

` string filePath = Path.GetTempFileName();

foreach (IFormFile imageFile in productModel.ProductPhotoFiles)
{
    if (imageFile.Length > 0)
    {
        using (FileStream stream = new FileStream(filePath, FileMode.Create))
        {
            imageFile.CopyTo(stream);
            using (dCore.Image.Logic.Image imageClass = new Image())
            {
                int productPhotoID;

                using (dCore.Commerce.Logic.ProductPhoto productPhotoClass = new dCore.Commerce.Logic.ProductPhoto())
                {
                    productPhotoID = productPhotoClass.Insert(newProduct.ID, userID);
                }

                //ERROR INSIDE THIS CALL
                Image<Rgba32> image = imageClass.Load(stream);
                image = imageClass.Crop(image, 3.22, 4);
                image = imageClass.Scale(image, 1080, null);
                imageClass.Save(image, _env.WebRootPath + "\\images\\products\\" + productPhotoID + ".jpg", ImageFormat.Jpg);
            }
        }
    }
}

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Bmp;
using SixLabors.ImageSharp.Formats.Gif;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.Primitives;

namespace dCore.Image.Logic
{
    public class Image : IDisposable
    {
        public Image<Rgba32> Load(Stream stream)
        {
            //ERROR IN THIS LINE
            return SixLabors.ImageSharp.Image.Load(stream);
        }
    }
}`

Images:
http://dev.avenida7.com/Admin/images/1.jpg
http://dev.avenida7.com/Admin/images/2.png
http://dev.avenida7.com/Admin/images/3.jpg

invalid question

Most helpful comment

Questions like this are best suited to our Gitter channel. That's why we have a template.

In any case the answer is problem is easily diagnosed.

imageFile.CopyTo(stream);

When you are copying to a stream you are leaving the stream position at the end of the stream not at the beginning. You need to set the position to 0.

As a side note, those abstractions you have written Crop, Scale are ruining a perfectly good API and I strongly recommend against writing code like this.

Thanks Jim, I found the solution in another post right after I did this one.

Regarding my abstractions, I really need them. They are only making some calculations (position, etc, that I need for my specific case), the image manipulation is being done inside those abstations by ImageSharp.

Thanks a lot for your help!

All 3 comments

Questions like this are best suited to our Gitter channel. That's why we have a template.

In any case the answer is problem is easily diagnosed.

imageFile.CopyTo(stream);

When you are copying to a stream you are leaving the stream position at the end of the stream not at the beginning. You need to set the position to 0.

As a side note, those abstractions you have written Crop, Scale are ruining a perfectly good API and I strongly recommend against writing code like this.

Questions like this are best suited to our Gitter channel. That's why we have a template.

In any case the answer is problem is easily diagnosed.

imageFile.CopyTo(stream);

When you are copying to a stream you are leaving the stream position at the end of the stream not at the beginning. You need to set the position to 0.

As a side note, those abstractions you have written Crop, Scale are ruining a perfectly good API and I strongly recommend against writing code like this.

Thanks Jim, I found the solution in another post right after I did this one.

Regarding my abstractions, I really need them. They are only making some calculations (position, etc, that I need for my specific case), the image manipulation is being done inside those abstations by ImageSharp.

Thanks a lot for your help!

Questions like this are best suited to our Gitter channel. That's why we have a template.
In any case the answer is problem is easily diagnosed.
imageFile.CopyTo(stream);
When you are copying to a stream you are leaving the stream position at the end of the stream not at the beginning. You need to set the position to 0.
As a side note, those abstractions you have written Crop, Scale are ruining a perfectly good API and I strongly recommend against writing code like this.

Thanks Jim, I found the solution in another post right after I did this one.

Regarding my abstractions, I really need them. They are only making some calculations (position, etc, that I need for my specific case), the image manipulation is being done inside those abstations by ImageSharp.

Thanks a lot for your help!

can u share your solution?

Was this page helpful?
0 / 5 - 0 ratings