Return to Snippet

Revision: 55745
at February 20, 2012 23:38 by Krummelz


Initial Code
protected void ProcessImageAndItsThumbnail(string url)
{
    //make memory streams to work with the image bytes
    MemoryStream imageStream = new MemoryStream();
    MemoryStream thumbStream = new MemoryStream();

    //using, so resources get disposed automagically
    //using the uploaded image (which might be huge)
    using (System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(url)))
    {
        //for the new resized image
        System.Drawing.Image newImage;

        //if the size is greater than 600 x 450, scale it down
        if ((img.Size.Width > 600) && (img.Size.Height > 450))
        {
            newImage = img.GetThumbnailImage(600, 450, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), System.IntPtr.Zero);
        }
        else
            newImage = img;

        //and then create a thumbnail too
        System.Drawing.Image thumb = img.GetThumbnailImage(200, 150, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), System.IntPtr.Zero);

        //for the new image
        Graphics g = Graphics.FromImage(newImage);
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
        g.DrawImage(img, 0, 0, newImage.Width, newImage.Height);

        //for the thumbnail
        Graphics gT = Graphics.FromImage(thumb);
        gT.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
        gT.DrawImage(img, 0, 0, thumb.Width, thumb.Height);

        //put the images into the memory streams
        newImage.Save(imageStream, ImageFormat.Jpeg);
        thumb.Save(thumbStream, ImageFormat.Jpeg);
    }


    //then, using a filestream, we write the file back to the same location, for the scaled down file
    int bufferSize = 32;
    byte[] buffer = new byte[bufferSize];
    long savedLength = 0;
    long imgLength = imageStream.Length;
    using (FileStream fs = new FileStream(Server.MapPath(url), FileMode.Create))
    {
        //write it back
        imageStream.Position = 0;
        while (savedLength < imgLength)
        {
            int bytes = imageStream.Read(buffer, 0, bufferSize);
            fs.Write(buffer, 0, bytes);
            savedLength += bytes;
        }
    }
    //and for the thumbnail
    savedLength = 0;
    imgLength = thumbStream.Length;
    //make a new URL for the thumbnail to be saved to
    string extention = url.Substring(url.Length - 4, 4);
    string thumb_url = url.Substring(0, url.Length - 4) + "_thumb" + extention;
    //save the thumbnail
    using (FileStream fs = new FileStream(Server.MapPath(thumb_url), FileMode.Create))
    {
        //write it back
        thumbStream.Position = 0;
        while (savedLength < imgLength)
        {
            int bytes = thumbStream.Read(buffer, 0, bufferSize);
            fs.Write(buffer, 0, bytes);
            savedLength += bytes;
        }
    }
}
protected bool ThumbnailCallback()
{
    return true; //can't remember why this is here
}

Initial URL


Initial Description
Just call "ProcessImageAndItsThumbnail" with your newly uploaded image's URL, and it will resize it for you, and make a thumbnail as well. Just remember to set the sizes you want for these new images.

Initial Title
C# image processing, useful for resizing images and making thumbnails

Initial Tags
images, c#

Initial Language
C#