in my code I take image as input. My user does not know much about technology , so they expect to give any image size they want , but I have limitations and I can't save larger images in my disc, so I want to compress images before saving them in my disc . I appreciate helping me with this compression.
That's how i solved my problem using Magick.NET library : i used the quality property to reduce the size and i did it in a loop , while the image size reaches my goal size :
MagickImage magickImge = new MagickImage(image);
int quality = magickImge.Quality;
long length = image.Length;
while (length > fileSizeInByte)
{
if (quality <= 6)
{
return Error;
}
quality = quality - ((quality * 15) / 100);
MagickImage lowQualityImage = new MagickImage(image);
lowQualityImage.Quality = quality;
byte[] newImageBytes = lowQualityImage.ToByteArray();
if (newImageBytes.Length <= fileSizeInByte)
{
magickImge.Quality = quality;
length = newImageBytes.Length;
}
}
i see that if the quality goes below 6 my pic will be illegible