Jasinski Technical Wiki


Home Page
All Pages

Quick Search
Advanced Search »

Contributor Links

Create a new Page
File Management
Your Profile

Other Wiki Sections



Thumbnails - Creating in C#

Modified on Fri, Oct 07, 2011, 4:16 PM by Administrator Categorized as ASP·NET Web Forms, ·Net Framework

Sample Usage

The following code creates the file C:\Images\MyImage_thumb_750.png, which has dimensions 75% of the original file.

const string file = @"C:\Images\MyImage.png";
var f = new ImageFile(file);

Reusable Code

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

public class ImageFile
    public string FileName { get; private set; }
    public ImageFile(string fileName)
        FileName = fileName;
    public void SaveThumbnail(double factor)
        var img = Image.FromFile(FileName);
        var width = (int)Math.Ceiling(img.Width * factor);
        var height = (int)Math.Ceiling(img.Height * factor);

        var thumb = img.GetThumbnailImage(width, height,
            new Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);

        var imageStream = new MemoryStream();
        thumb.Save(imageStream, ImageFormat);
        var imageContent = new byte[imageStream.Length];
        imageStream.Position = 0;
        imageStream.Read(imageContent, 0, (int)imageStream.Length);
        File.WriteAllBytes(GetThumbnailFileName(FileName, factor), imageContent);
    public ImageFormat ImageFormat
            var ext = Path.GetExtension(FileName).Substring(1).ToUpper();
                case "BMP": return ImageFormat.Bmp;
                case "EMF": return ImageFormat.Emf;
                case "GIF": return ImageFormat.Gif;
                case "ICO": return ImageFormat.Icon;
                case "JPEG": return ImageFormat.Jpeg;
                case "JPG": return ImageFormat.Jpeg;
                case "JPE": return ImageFormat.Jpeg;
                case "JFIF": return ImageFormat.Jpeg;
                case "PNG": return ImageFormat.Png;
                case "TIFF": return ImageFormat.Tiff;
                case "TIF": return ImageFormat.Tiff;
                case "WMF": return ImageFormat.Wmf;
                default: throw new Exception("Invalid file type: " + ext);

    /*= Private Members =====================================================================*/
    private bool ThumbnailCallback()
        return true;

    /*= Static Members ======================================================================*/
    public static string GetThumbnailFileName(string fileName, double factor)
        var dir = Path.GetDirectoryName(fileName);
        var name = Path.GetFileNameWithoutExtension(fileName);
        var ext = Path.GetExtension(fileName);
        var f = Math.Round(factor * 1000, 0).ToString();
        return Path.Combine(dir, name + "_thumb_" + f + ext);


Taken from here: http://www.csharp-station.com/Articles/Thumbnails.aspx

/// <summary>
/// Creates a thumbnail image from a file spec in the calling URL.
/// </summary>
public class MakeThumbnail : System.Web.UI.Page
    private void Page_Load(object sender, System.EventArgs e)
        // get the file name -- fall800.jpg
        string file = Request.QueryString["file"];

        // create an image object, using the filename we just retrieved
        System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(file));

        // create the actual thumbnail image
        System.Drawing.Image thumbnailImage = 
            image.GetThumbnailImage(64, 64, new System.Drawing.Image.GetThumbnailImageAbort(
                ThumbnailCallback), IntPtr.Zero);

        // make a memory stream to work with the image bytes
        MemoryStream imageStream = new MemoryStream();

        // put the image into the memory stream
        thumbnailImage.Save(imageStream, System.Drawing.Imaging.Imageformat.Jpeg);

        // make byte array the same size as the image
        byte[] imageContent = new Byte[imageStream.Length];

        // rewind the memory stream
        imageStream.Position = 0;

        // load the byte array with the image
        imageStream.Read(imageContent, 0, (int)imageStream.Length);

        // return byte array to caller with image type
        Response.ContentType = "image/jpeg";

    /// <summary>
    /// Required, but not used
    /// </summary>
    /// <returns>true</returns>
    public bool ThumbnailCallback()
        return true;

    // ... non-applicable infrastructure code removed for clarity ...


ScrewTurn Wiki version Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2023, Patrick Jasinski.