To generate an image thumbnail in ASP.NET you can use the method below.
public static void CreateThumbnail(string filename, int desiredWidth, int desiredHeight, string outFilename)
{
using (System.Drawing.Image img = System.Drawing.Image.FromFile(filename))
{
float widthRatio = (float)img.Width / (float)desiredWidth;
float heightRatio = (float)img.Height / (float)desiredHeight;
float ratio = heightRatio > widthRatio ? heightRatio : widthRatio;
int newWidth = Convert.ToInt32(Math.Floor((float)img.Width / ratio));
int newHeight = Convert.ToInt32(Math.Floor((float)img.Height / ratio));
using (System.Drawing.Image thumb = img.GetThumbnailImage(newWidth, newHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailImageAbortCallback), IntPtr.Zero))
{
thumb.Save(outFilename, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
public static bool ThumbnailImageAbortCallback()
{
return true;
}
You can use the way resize an image to create a thumbnail in c#.
public void GenerateImage(byte[] data, string extension, string filename)
{
using (MemoryStream ms = new MemoryStream(data))
{
var image = Image.FromStream(ms);
image.Save(filename);
ResizeImage(image, 800, 600, "large-image.jpg");
ResizeImage(image, 480, 320, "medium-image.jpg");
ResizeImage(image, 192, 144, "small-image.jpg");
}
}
public void ResizeImage(Image image, int width, int height, string filename)
{
using (Bitmap newImage = new Bitmap(width, height))
{
var graphics = Graphics.FromImage(newImage);
graphics.DrawImage(image, 0, 0, width, height);
newImage.Save(filename, ImageFormat.Jpeg);
}
}