Create Thumbnail : Image « 2D Graphics « C# / C Sharp






Create Thumbnail

   
// crudwork
// Copyright 2004 by Steve T. Pham (http://www.crudwork.com)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with This program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace crudwork.Utilities
{
  /// <summary>
  /// Image Utility
  /// </summary>
  public static class ImageUtil
  {
    #region Enumerators
    #endregion

    #region Fields
    private static int thumbWidth = 96;
    private static int thumbHeight = 96;
    #endregion

    #region Constructors
    #endregion

    #region Event Methods

    #region System Event Methods
    #endregion

    #region Application Event Methods
    #endregion

    #region Custom Event Methods
    #endregion

    #endregion

    #region Public Methods
    /// <summary>
    /// Create thumbnail from an image filename.
    /// </summary>
    /// <param name="filename"></param>
    /// <returns></returns>
    public static Image GetThumbnail(string filename)
    {
      return GetThumbnail(Image.FromFile(filename), thumbWidth, thumbHeight);
    }

    /// <summary>
    /// Create thumbnail with the given size from an image filename.
    /// </summary>
    /// <param name="filename"></param>
    /// <param name="width"></param>
    /// <param name="height"></param>
    /// <returns></returns>
    public static Image GetThumbnail(string filename, int width, int height)
    {
      return GetThumbnail(Image.FromFile(filename), width, height);
    }

    /// <summary>
    /// Create thumbnail from an Image type.
    /// </summary>
    /// <param name="image"></param>
    /// <returns></returns>
    public static Image GetThumbnail(Image image)
    {
      return GetThumbnail(image, thumbWidth, thumbHeight);
    }

    /// <summary>
    /// Create thumbnail with the given size for an Image type.
    /// </summary>
    /// <param name="image"></param>
    /// <param name="width"></param>
    /// <param name="height"></param>
    /// <returns></returns>
    public static Image GetThumbnail(Image image, int width, int height)
    {
      if (image == null)
        throw new ArgumentNullException("image");

      return image.GetThumbnailImage(width, height, null, new IntPtr());
    }

    /// <summary>
    /// Read the binary content from a file into a byte array.
    /// </summary>
    /// <param name="filename"></param>
    /// <returns></returns>
    public static byte[] FileToByteArray(string filename)
    {
      using (Stream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
      {
        byte[] imageData = new Byte[fs.Length];
        fs.Read(imageData, 0, imageData.Length);
        fs.Close();
        return imageData;
      }
    }

    /// <summary>
    /// Convert a byte array to Image type.
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static Image ByteArrayToImage(byte[] value)
    {
      if ((value == null) || (value.Length == 0))
        throw new ArgumentNullException("value");

      return Image.FromStream(new MemoryStream(value));
    }

    /// <summary>
    /// Create a thumbnail image from an image filename.
    /// </summary>
    /// <param name="filename"></param>
    /// <returns></returns>
    public static byte[] CreateThumbnail(string filename)
    {
      using (MemoryStream s = new MemoryStream())
      using (Image image = Image.FromFile(filename).GetThumbnailImage(thumbWidth, thumbHeight, null, new IntPtr()))
      {
        image.Save(s, ImageFormat.Jpeg);
        return s.ToArray();
      }
    }

    /// <summary>
    /// Create a thumbnail image from a byte array, presumely from an image. 
    /// </summary>
    /// <param name="imageData"></param>
    /// <returns></returns>
    public static byte[] CreateThumbnail(byte[] imageData)
    {
      using (MemoryStream s = new MemoryStream())
      using (Image image = Image.FromStream(new MemoryStream(imageData)).GetThumbnailImage(thumbWidth, thumbHeight, null, new IntPtr()))
      {
        image.Save(s, ImageFormat.Jpeg);
        return s.ToArray();
      }
    }

    /// <summary>
    /// Convert an image to a byte array
    /// </summary>
    /// <param name="imageToConvert"></param>
    /// <param name="formatOfImage"></param>
    /// <returns></returns>
    public static byte[] ConvertImageToByteArray(Image imageToConvert, ImageFormat formatOfImage)
    {
      try
      {
        using (MemoryStream ms = new MemoryStream())
        {
          imageToConvert.Save(ms, formatOfImage);
          return ms.ToArray();
        }
      }
      catch (Exception ex)
      {
        
        throw;
      }
    }

    /// <summary>
    /// Return the ImageFormat type based on the file's extension
    /// </summary>
    /// <param name="filename"></param>
    /// <returns></returns>
    public static ImageFormat GetImageFormat(string filename)
    {
      string ext = new FileInfo(filename).Extension.Substring(1);
      switch (ext.ToUpper())
      {
        case "BMP":
          return ImageFormat.Bmp;
        //case "":
        //  return ImageFormat.Emf;
        //case "":
        //  return ImageFormat.Exif;
        case "GIF":
          return ImageFormat.Gif;
        case "ICO":
          return ImageFormat.Icon;
        case "JPEG":
        case "JPG":
        case "JPE":
          return ImageFormat.Jpeg;
        //case "":
        //  return ImageFormat.MemoryBmp;
        case "PNG":
          return ImageFormat.Png;
        case "TIF":
        case "TIFF":
          return ImageFormat.Tiff;
        case "WMF":
          return ImageFormat.Wmf;
        default:
          return null;

      }
    }
    #endregion

    #region Private Methods
    #endregion

    #region Protected Methods
    #endregion

    #region Properties
    #endregion

    #region Others
    #endregion
  }
}

   
    
    
  








Related examples in the same category

1.Load Image from an image file with Exception handler
2.Image.FromStream: load image from stream
3.Shrink ImageShrink Image
4.Shear ImageShear Image
5.Clone ImageClone Image
6.Get Image Resolution and Image size and Display sizeGet Image Resolution and Image size and Display size
7.Draw image based on its sizeDraw image based on its size
8.Set image resolution and paint itSet image resolution and paint it
9.Load image and displayLoad image and display
10.Fill Ellipse with image based Texture BrushFill Ellipse with image based Texture Brush
11.Image Save
12.Image Open
13.Thumbnail Image
14.Draw on Pixel-Size Image
15.Draw text on an Image
16.Partial Image Rotate
17.Partial Image Stretch
18.Draw Partial Image
19.Image At Points (Draw part of the image)
20.Image Reflection
21.Image Scale Isotropic
22.Image Scale To Rectangle
23.Center Pixel-Size Image by using Image.Width and Image.Height
24.Center an Image (VerticalResolution, HorizontalResolution)
25.Load image from a URL(Web) and draw it
26.Scribble with Bitmap
27.Image Flipping and Rotating
28.Image.GetThumbnailImage
29.Image Zoom
30.Image Resize
31.Get Bitmap Source
32.Get Bytes From BitmapSource
33.Load Image
34.Create Icon from Image
35.Resizes an image
36.Get Gravatar Image
37.Changing image opacity, Resize image, crop image and gray scale image
38.Resize Image
39.Resize image to fit
40.Image Filter
41.Save Window/Canvas to Image
42.Image Cache
43.Saves the image to a bytes array. Used to store an image into DB.
44.Get the "energy" of a pixel