Skip to content

Bad sampling factor when using SaveAsJpeg #1076

@timyboy12345

Description

@timyboy12345

Description

I am using ImageSharp to upload/download images from the web, and save them locally. Almost all images are downloaded succesfully, but a few give the error "Bad sampling factor". One of these images is this image of a table. I have no idea why these fail and what's different about the failing images compared to working images.

Steps to Reproduce

I am using the SaveAsJpeg() function with a FileStream that gets a file from Image.Load(). The file is downloaded from the internet and some checks are performed first (eg. check file type). After this, the ImageSharp is used to resize the image (if necessary) and the file is saved locally. See the full code below for all important parts of this process:

file = await client.DownloadDataTaskAsync(new Uri(url));
using var ms = new System.IO.MemoryStream(file);

...

using (Image image = Image.Load(file))
                {
                    if (image.Width > maxWidth || image.Height > maxHeight)
                    {
                        image.Mutate(x => x.Resize(new ResizeOptions { Mode = ResizeMode.Max, Size = new SixLabors.Primitives.Size(maxWidth, maxHeight) }));
                    }

                    using (var outputStream = new FileStream(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images", randomName), FileMode.CreateNew))
                    {
                        image.SaveAsJpeg(outputStream, new SixLabors.ImageSharp.Formats.Jpeg.JpegEncoder { Quality = 100 });
                    };

                    width = image.Width;
                    height = image.Height;
                }

System Configuration

I am running this application in

  • ImageSharp version: V1.0.0-beta0007
  • Environment (Operating system, version and so on): Windows 10 version 1909
  • .NET Framework version: .NET Core 3.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions