Resizing an image in asp.net without losing the image quality
I had the same problem a while back and dealt with it this way:
private Image RezizeImage(Image img, int maxWidth, int maxHeight)
{
if(img.Height < maxHeight && img.Width < maxWidth) return img;
using (img)
{
Double xRatio = (double)img.Width / maxWidth;
Double yRatio = (double)img.Height / maxHeight;
Double ratio = Math.Max(xRatio, yRatio);
int nnx = (int)Math.Floor(img.Width / ratio);
int nny = (int)Math.Floor(img.Height / ratio);
Bitmap cpy = new Bitmap(nnx, nny, PixelFormat.Format32bppArgb);
using (Graphics gr = Graphics.FromImage(cpy))
{
gr.Clear(Color.Transparent);
// This is said to give best quality when resizing images
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.DrawImage(img,
new Rectangle(0, 0, nnx, nny),
new Rectangle(0, 0, img.Width, img.Height),
GraphicsUnit.Pixel);
}
return cpy;
}
}
private MemoryStream BytearrayToStream(byte[] arr)
{
return new MemoryStream(arr, 0, arr.Length);
}
private void HandleImageUpload(byte[] binaryImage)
{
Image img = RezizeImage(Image.FromStream(BytearrayToStream(binaryImage)), 103, 32);
img.Save("IMAGELOCATION.png", System.Drawing.Imaging.ImageFormat.Gif);
}
I just read that this was the the way to get highest quality.
How to resize image without losing quality
I'm using the following method for thousands of images and it never loses significant quality or results in a dotted image.
public static Image ScaleImage(Image image, int height)
{
double ratio = (double)height/ image.Height;
int newWidth = (int)(image.Width * ratio);
int newHeight = (int)(image.Height * ratio);
Bitmap newImage = new Bitmap(newWidth, newHeight);
using (Graphics g = Graphics.FromImage(newImage))
{
g.DrawImage(image, 0, 0, newWidth, newHeight);
}
image.Dispose();
return newImage;
}
I've taken the liberty of scaling the image you posted using this code to 128px (Like the thumbnail you posted).
Result:
How resize image without losing quality
I use this method as a way to get a thumbnail image (of any size) from an original (of any size). Note that there are inherent issues when you ask for a size ratio that varies greatly from that of the original. Best to ask for sizes that are in scale to one another:
public static Image GetThumbnailImage(Image OriginalImage, Size ThumbSize)
{
Int32 thWidth = ThumbSize.Width;
Int32 thHeight = ThumbSize.Height;
Image i = OriginalImage;
Int32 w = i.Width;
Int32 h = i.Height;
Int32 th = thWidth;
Int32 tw = thWidth;
if (h > w)
{
Double ratio = (Double)w / (Double)h;
th = thHeight < h ? thHeight : h;
tw = thWidth < w ? (Int32)(ratio * thWidth) : w;
}
else
{
Double ratio = (Double)h / (Double)w;
th = thHeight < h ? (Int32)(ratio * thHeight) : h;
tw = thWidth < w ? thWidth : w;
}
Bitmap target = new Bitmap(tw, th);
Graphics g = Graphics.FromImage(target);
g.SmoothingMode = SmoothingMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.High;
Rectangle rect = new Rectangle(0, 0, tw, th);
g.DrawImage(i, rect, 0, 0, w, h, GraphicsUnit.Pixel);
return (Image)target;
}
resize image without losing quality MVC 4
Here is a code I use. Call ResizeImage
method in your action.
public class Size
{
public Size(int width, int height)
{
Width = width;
Height = height;
}
public int Width { get; set; }
public int Height { get; set; }
}
public static bool ResizeImage(string orgFile, string resizedFile, ImageFormat format, int width, int height)
{
try
{
using (Image img = Image.FromFile(orgFile))
{
Image thumbNail = new Bitmap(width, height, img.PixelFormat);
Graphics g = Graphics.FromImage(thumbNail);
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
Rectangle rect = new Rectangle(0, 0, width, height);
g.DrawImage(img, rect);
thumbNail.Save(resizedFile, format);
}
return true;
}
catch (Exception)
{
return false;
}
}
Related Topics
In C# Check That Filename Is *Possibly* Valid (Not That It Exists)
Differencebetween a Property and a Variable
Why Is There No Char.Empty Like String.Empty
Declaration Suffix for Decimal Type
How to Read Data from a Zip File Without Having to Unzip the Entire File
C# - Detect Time of Last User Interaction with the Os
Appsettings Get Value from .Config File
Failed to Serialize the Response in Web API
What Is Quicker, Switch on String or Elseif on Type
How to "Flatten" or "Index" 3D-Array in 1D Array
Suppressing "Is Never Used" and "Is Never Assigned To" Warnings in C#
Linq to SQL Using Group by and Count(Distinct)
Authorization Header Is Lost on Redirect
Using Static Variables Instead of Application State in ASP.NET
Zoom and Translate an Image from the Mouse Location
Why Does the Contains() Operator Degrade Entity Framework's Performance So Dramatically
Cannot Set Some Http Headers When Using System.Net.Webrequest