Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Page History: Thumbnails - Creating in C#

Compare Page Revisions



« Older Revision - Back to Page History - Current Revision


Page Revision: Fri, Oct 07, 2011, 4:16 PM


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);
f.SaveThumbnail(0.75);

Reusable Code



Source

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";
        Response.BinaryWrite(imageContent);
    }

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

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

}

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