Skip to content

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

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:

Activity

  1. changed the title [-]resize image error[/-] [+]resize an image from byte[] to byte[] may lose pixels[/+] on Jan 20, 2018
  2. JimBobSquarePants commented on Jan 20, 2018

    @JimBobSquarePants
    Member

    Hi @endink

    I'm able to resize and save the image to 150 x150 without issue. Are you sure all your variables are correct?

    splash

  3. endink commented on Jan 20, 2018

    @endink
    Author

    @JimBobSquarePants well, I'll check my code again, and if possible, I'll extract my main code and package it up and upload it.

  4. endink commented on Jan 20, 2018

    @endink
    Author

    @JimBobSquarePants sorry, it works well now, my code writes less bytes when I write a byte array to a file

  5. JimBobSquarePants commented on Jan 20, 2018

    @JimBobSquarePants
    Member

    No worries, glad you found a solution. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions