Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 of 4 tasks
endink opened this issue Jan 20, 2018 · 4 comments
Closed
2 of 4 tasks

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

endink opened this issue Jan 20, 2018 · 4 comments

Comments

@endink
Copy link

endink commented Jan 20, 2018

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:
@endink endink changed the title resize image error resize an image from byte[] to byte[] may lose pixels Jan 20, 2018
@JimBobSquarePants
Copy link
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

@endink
Copy link
Author

endink commented Jan 20, 2018

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

@endink
Copy link
Author

endink commented Jan 20, 2018

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

@endink endink closed this as completed Jan 20, 2018
@JimBobSquarePants
Copy link
Member

No worries, glad you found a solution. 👍

@JimBobSquarePants JimBobSquarePants mentioned this issue Dec 4, 2016
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants