I'm trying to decrease images dimensions, example 100px X 100px to 50px X 50px. I try some code but it increases the size of image, example 270kb to 700kb. Any idea?
I try two methods and have the same result:
public void ResizeImage()
{
Image img = Image.FromFile("~/Images/image.jpg");
double imgHeight = img.Size.Height;
double imgWidth = img.Size.Width;
double x = 0.5;
//New sizes
int newWidth = Convert.ToInt32(imgWidth * x);
int newHeight = Convert.ToInt32(imgHeight * x);
Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);
Image myThumbnail = img.GetThumbnailImage(newWidth, newHeight, myCallback, IntPtr.Zero);
//Save
myThumbnail.Save("~/Images/image.jpg");
}
public bool ThumbnailCallback()
{
return false;
}
public void ResizeImage2()
{
Image img = Image.FromFile("~/Images/image.jpg");
double imgHeight = img.Size.Height;
double imgWidth = img.Size.Width;
double x = 0.5;
//Seteo nuevos tamaños
int newWidth = Convert.ToInt32(imgWidth * x);
int newHeight = Convert.ToInt32(imgHeight * x);
Bitmap b = new Bitmap(newWidth, newHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(img, 0, 0, newWidth, newHeight);
g.Dispose();
b.Save("~/Images/image.jpg");
}
Thanks