saucecontrol / PhotoSauce

MagicScaler high-performance, high-quality image processing pipeline for .NET
http://photosauce.net/
MIT License
589 stars 49 forks source link

EncoderInfo not set correctly if output to Stream #134

Closed nquandt closed 11 months ago

nquandt commented 11 months ago

Given some image.. and EncoderOptions set to Webp type.. The output stream is not encoded correctly unless I manually call TrySetEncoderFormat.. (where I was under the impression this should be set based on the EncoderOptions)

var newStream = new MemoryStream();

var settings = new ProcessImageSettings()
    {        
        EncoderOptions = new WebpLossyEncoderOptions(80), 
    };
MagicImageProcessor.ProcessImage("./image.webp", newStream, settings); // load an image however..

var bytes1 = newStream.ToArray();

var newStream2 = new MemoryStream();

var settings = new ProcessImageSettings()
    {        
        EncoderOptions = new WebpLossyEncoderOptions(80), 
    };

settings.TrySetEncoderFormat("image/webp"); // this line makes it different
MagicImageProcessor.ProcessImage("./image.webp", newStream2 , settings); // load an image however..

var bytes2 = newStream2.ToArray();

Assert.Equal(bytes1.Length, bytes2.Length); // returns false (should be true)

If I output to a file path instead of a stream it sets the encoder correctly (i believe because of this line)

ctx.Settings.EncoderInfo ??= getEncoderFromPath(outPath);