Giter VIP home page Giter VIP logo

Comments (14)

Bartmax avatar Bartmax commented on May 18, 2024 1

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);
        }

    }
}

from imagesharp.

JimBobSquarePants avatar JimBobSquarePants commented on May 18, 2024 1

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.

from imagesharp.

tocsoft avatar tocsoft commented on May 18, 2024 1

its a public static readonly field on ImageFormats
https://github.com/SixLabors/ImageSharp/blob/master/src/ImageSharp/ImageFormats.cs#L18

from imagesharp.

JimBobSquarePants avatar JimBobSquarePants commented on May 18, 2024

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).

from imagesharp.

Bartmax avatar Bartmax commented on May 18, 2024

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 ?

from imagesharp.

Bartmax avatar Bartmax commented on May 18, 2024

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!

from imagesharp.

Bartmax avatar Bartmax commented on May 18, 2024

thank you @JimBobSquarePants for your help!
I will keep my approach then.

from imagesharp.

JimBobSquarePants avatar JimBobSquarePants commented on May 18, 2024

No worries 😄

from imagesharp.

davidliang2008 avatar davidliang2008 commented on May 18, 2024

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!

from imagesharp.

JimBobSquarePants avatar JimBobSquarePants commented on May 18, 2024

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

from imagesharp.

Bartmax avatar Bartmax commented on May 18, 2024

How would one detect the format now? or if it's Jpeg set the quality?

I don't like imageFormat.Name == "JPEG" very much

from imagesharp.

tocsoft avatar tocsoft commented on May 18, 2024

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.

from imagesharp.

Bartmax avatar Bartmax commented on May 18, 2024

ImageFormats.Jpeg is internal sealed ?

from imagesharp.

Bartmax avatar Bartmax commented on May 18, 2024

oh thank you!

from imagesharp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.