Skip to content

resize an image from byte[] to byte[] may lose pixels #444

@endink

Description

@endink

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of ImageSharp
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

Description

Resize an image will lost some pixel.

Steps to Reproduce

btw: _imageData is an image byte array.

public override byte[] Scale(double scaleFactor, String format)
{
    this.ThrowIfDisposed();
    if (scaleFactor <= 0)
    {
        throw new ArgumentException("scalefactor must be greate than 0", nameof(scaleFactor));
    }
    try
    {
        int width = (int)(this.Profile.PixelWidth * scaleFactor);
        int height = (int)(this.Profile.PixelHeight * scaleFactor);
        using (Image<Rgba32> bitmap = Image.Load(_imageData, out IImageFormat imageFormat))
        {
            bitmap.Mutate(x => x.Resize(width, height));
            IImageFormat f = String.IsNullOrWhiteSpace(format) ? imageFormat : GetImageFormat(format);
            using (MemoryStream stream = new MemoryStream())
            {
                bitmap.Save(stream, f);
                        
                stream.Position = 0;
                return stream.ToArray();
            }
        }
    }
    catch (Exception ex)
    {
        throw new ImagingException("scale error.", ex);
    }
}


private IImageFormat GetImageFormat(string format)
{
    IImageFormat imageFormat = SixLabors.ImageSharp.Configuration.Default.FindFormatByFileExtension(format);
    if (imageFormat == null)
    {
        throw new ImagingException($"unsupported format:{format}。");
    }
    return imageFormat;
}

original Image

gounda_takeshi

got result:

resize to 150px:
avatar_150

resize to 80px
avatar_80

resize to 30px
avatar_30

System Configuration

Visual studio 2017

 <TargetFramework>netstandard1.6</TargetFramework>
  • ImageSharp version: 1.0.0-beta0002
  • Environment (Operating system, version and so on): Windows 10 x64
  • .NET Framework version: netstandard1.6
  • Additional information:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions