How can I decrease image size as low as possible with resize? #1521
-
Dear Supporters, And I use this code to resize it: public byte[] ResizeImage(byte[] imageByteArray, int? maxHeight = null, int? maxWidth = null, string imageFormat = "image/jpeg")
{
using (var original = Image.Load(imageByteArray))
{
int newWidth = original.Width;
int newHeight = original.Height;
if (maxWidth < original.Width || maxHeight < original.Height)
{
var ratioX = (double)(maxWidth ?? original.Width) / original.Width;
var ratioY = (double)(maxHeight ?? original.Height) / original.Height;
var ratio = Math.Min(ratioX, ratioY);
newWidth = (int)(original.Width * ratio);
newHeight = (int)(original.Height * ratio);
}
original.Mutate(x => x.Resize(newWidth, newHeight));
using (var ms = new MemoryStream())
{
switch (imageFormat)
{
case "image/jpeg":
original.SaveAsJpeg(ms);
break;
case "image/png":
original.SaveAsPng(ms);
break;
case "image/gif":
original.SaveAsGif(ms);
break;
default:
throw new NotSupportedException($"Image format {imageFormat} not supported!");
}
return ms.ToArray();
}
}
} ImageByteArray is the linked image in bytes. Maxheight is 55, and MaxWidth is 55 also. Finally from the original 640480 ~700kbyte image, I got an 5541 ~560kbyte image. How the hell can I resize this image to a few kbyte size? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
@martonx if Can you try a simplified code, which takes an input jpeg and resizes it exactly to 50x50, and check the output size? |
Beta Was this translation helpful? Give feedback.
-
It's got a massive ICC profile embedded in it. Using the following code to resize and strip the profile. using (var image = Image.Load(Path.Combine(inPath, "discussions.jpg")))
{
var options = new ResizeOptions { Mode = ResizeMode.Max, Size = new Size(55, 55) };
image.Mutate(x => x.Resize(options));
image.Metadata.IccProfile = null;
image.Save(Path.Combine(outPath, "discussions-no-meta.jpg"));
} A jpeg of 55x41px is 3.07kb |
Beta Was this translation helpful? Give feedback.
It's got a massive ICC profile embedded in it.
Using the following code to resize and strip the profile.
A jpeg of 55x41px is 3.07kb