// 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 .
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace crudwork.Utilities
{
///
/// Image Utility
///
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
///
/// Create thumbnail from an image filename.
///
///
///
public static Image GetThumbnail(string filename)
{
return GetThumbnail(Image.FromFile(filename), thumbWidth, thumbHeight);
}
///
/// Create thumbnail with the given size from an image filename.
///
///
///
///
///
public static Image GetThumbnail(string filename, int width, int height)
{
return GetThumbnail(Image.FromFile(filename), width, height);
}
///
/// Create thumbnail from an Image type.
///
///
///
public static Image GetThumbnail(Image image)
{
return GetThumbnail(image, thumbWidth, thumbHeight);
}
///
/// Create thumbnail with the given size for an Image type.
///
///
///
///
///
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());
}
///
/// Read the binary content from a file into a byte array.
///
///
///
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;
}
}
///
/// Convert a byte array to Image type.
///
///
///
public static Image ByteArrayToImage(byte[] value)
{
if ((value == null) || (value.Length == 0))
throw new ArgumentNullException("value");
return Image.FromStream(new MemoryStream(value));
}
///
/// Create a thumbnail image from an image filename.
///
///
///
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();
}
}
///
/// Create a thumbnail image from a byte array, presumely from an image.
///
///
///
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();
}
}
///
/// Convert an image to a byte array
///
///
///
///
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;
}
}
///
/// Return the ImageFormat type based on the file's extension
///
///
///
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
}
}