I'm having an issue where my original jpg of quality 90 (photoshop thing) is resized it gets a worse quality than if I resize in photoshop.
Is there anyway to improve this or maybe I'm missing to set the quality properly?
How are you using the library?
The default quality of the Jpeg encoder is 75, which is settable.
I doubt very much this has anything to do with the resize algorithms. (Which are configurable to suite your needs).
I'm using it like this:
private async Task ResizeAsync(string source, string destination, int width, int height)
{
using (var sourceStream = await fileManager.GetStreamAsync(source))
{
var image = new Image(sourceStream);
ResizeOptions options = new ResizeOptions
{
Mode = ResizeMode.Crop,
Size = new Size(width, height)
};
using (var destinationStream = new MemoryStream())
{
image.Resize(options)
.Save(destinationStream);
destinationStream.Seek(0, SeekOrigin.Begin);
await fileManager.CreateFileAsync(destination, destinationStream);
}
}
}
If I do
.SaveAsJpg(destinationStream, 90)
the quality is improved as I see in Photoshop, so question is: How do I set the JPGEncoder to save as 90 quality as default ?
I tryied:
image.Quality = 80;
and
images.CurrentImageEncoder.Quality = 80;
without luck, so I'm using a workaround right now:
if (image.CurrentImageFormat.Encoder is JpegEncoder)
{
image.Resize(options)
.SaveAsJpeg(destinationStream, 80);
}
else
{
image.Resize(options)
.Save(destinationStream);
}
If there's a better way to set the JPEGEncoder quality I will be glad to know ;)
Thanks!
Hmmmm.... Setting the Quality property on the Image class should work.
This property will most likely disappear pretty soon though as we refactor the codec API to make it more sensible (see #26) so the approach you are taking will be a little more future proof.
thank you @JimBobSquarePants for your help!
I will keep my approach then.
No worries 馃槃
Hi @JimBobSquarePants : Would it be helpful if you can move Quality field from each different EncoderOptions to its base class?
Currently I am using ImageSharp (version: 1.0.0-alpha9-00148) to crop and resize images from Azure blob storage and save them back. And I really don't care whether they're .gif, .jpeg, .png and other image types. I just want to maximize the image quality before saving them back to Steam.
public Tuple<string, byte[]> CropAndResize(byte[] imageBytes, int offsetX, int offsetY,
int croppedWidth, int croppedHeight, int finalWidth, int finalHeight)
{
using (var image = Image.Load(imageBytes))
{
var croppedImage = image.Crop(new Rectangle(offsetX, offsetY, croppedWidth, croppedHeight))
.Resize(finalWidth, finalHeight);
var currentFormat = croppedImage.CurrentImageFormat;
var currentEncoder = currentFormat.Encoder;
using (var ms = new MemoryStream())
{
if (currentEncoder is JpegEncoder)
{
croppedImage.SaveAsJpeg(ms, new JpegEncoderOptions { Quality = 100 });
}
else if (currentEncoder is PngEncoder)
{
croppedImage.SaveAsPng(ms, new PngEncoderOptions { Quality = 100 });
}
else if (currentEncoder is GifEncoder)
{
croppedImage.SaveAsGif(ms, new GifEncoderOptions { Quality = 100 });
}
else
{
croppedImage.Save(ms);
}
return Tuple.Create(currentFormat.Extension, ms.ToArray());
}
}
}
The .Save() method used to take an optional Quality parameter but I guess it got removed now.
By the way, I am very appreciated for this wonderful library, especially with .Net Core support!
Hi @davidliang2008 Glad you're a fan of our work 馃憤
We're actually gonna refactor the save methods. As part of this Quality will get dropped from everything but jpeg anyway since they're not really controlling the quality of the output, rather the color palette.
Cheers
James
How would one detect the format now? or if it's Jpeg set the quality?
I don't like imageFormat.Name == "JPEG" very much
For testing for Jpeg you probably want to do this imageFormat == ImageFormats.Jpeg instead of testing the name test against the global static format instance.
If you want to default all Jpegs to be saved out with a particular set of options you can override the default encoder. see the ChangeDefaultEncoderOptions sample. You can do the same for changing the decoders too.
ImageFormats.Jpeg is internal sealed ?
its a public static readonly field on ImageFormats
https://github.com/SixLabors/ImageSharp/blob/master/src/ImageSharp/ImageFormats.cs#L18
oh thank you!