Comments (14)
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.
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.
its a public static readonly field on ImageFormats
https://github.com/SixLabors/ImageSharp/blob/master/src/ImageSharp/ImageFormats.cs#L18
from imagesharp.
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.
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.
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.
thank you @JimBobSquarePants for your help!
I will keep my approach then.
from imagesharp.
No worries 😄
from imagesharp.
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.
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.
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.
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.
ImageFormats.Jpeg
is internal sealed ?
from imagesharp.
oh thank you!
from imagesharp.
Related Issues (20)
- Image.Load Error HOT 1
- ImageSharp is not supporting negative values on writing EXIF MetaData HOT 1
- Saving webp to jpg gives strange result HOT 5
- sixlabors.imagesharp is missing NuGet package README file
- Unable to generate a thumbnail as expected HOT 10
- CR2: System.NotSupportedException: Missing SOI marker offset for tiff with old jpeg compression HOT 2
- NullReferenceException when trying to load an animated PNG HOT 1
- Black Background after resizing in the latest version. HOT 4
- AccessViolationException and hard crash with animated webp HOT 8
- Simple conversion of Tiff file to PNG or JPG generates a strange result, even, at certain times, it is in 3D perspective HOT 6
- Please backport CVE-2024-27929 to 2.1.x HOT 20
- no vulnerability free version for netstandard or netframework 4.7 HOT 1
- Cannot update to v2.1.7 due to vulnerability tag HOT 4
- Exception SixLabors.ImageSharp.ImageFormatException: 'reserved bytes should be zero' when using Image.LoadAsync HOT 7
- Exif rotation and save HOT 4
- Error SixLabors.ImageSharp.ImageFormatException: 'Unexpected chunk followed VP8X header' HOT 3
- File (detected as bmp) fails to open, consumes a lot of memory HOT 4
- regression on latest.. 3 vs 2 with GIF HOT 6
- APNG Decoder incorrectly handles frame offsets and dispose previous with blend over HOT 3
- Hard crash (Internal CLR error) with corrupt PNG image HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from imagesharp.