Tuesday, July 29, 2008

resize image automatically cool class

Dynamic Image resize:

It's really good class through this class you can handle resizing images with single function.


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

namespace DAL
{
public class ImageClass
{
#region Private Variable
private Stream _imgstream;
private int _Height, _Width, _destHeight, _destWidth;
private System.Drawing.Image _InputImage;
#endregion

#region Public Property
public System.Drawing.Image InputImage
{
get { return _InputImage; }
set { _InputImage = value; }
}
public int Height
{
get { return _Height; }
set { _Height = value; }
}
public int Width
{
get { return _Width; }
set { _Width = value; }
}
public int DestHeight
{
get { return _destHeight; }
set { _destHeight = value; }
}
public int DestWidth
{
get { return _destWidth; }
set { _destWidth = value; }
}
public Stream ImageStream
{
get { return _imgstream; }
set { _imgstream = value; }
}
#endregion

#region Resize Image From Stream
public System.Drawing.Image ResizeImage()
{
//try
//{
System.Drawing.Image originalImage = System.Drawing.Image.FromStream(_imgstream);
int sourceWidth = originalImage.Width;
int sourceHeight = originalImage.Height;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
if (sourceWidth <= _Width && sourceHeight <= _Height)
{
return originalImage;
}
else
{
/* for photo Thumb */
nPercentW = ((float)_Width / (float)sourceWidth);
nPercentH = ((float)_Height / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = (int)((_Height - (sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = (int)((_Width - (sourceHeight * nPercent)) / 2);
}
_destWidth = (int)(sourceWidth * nPercent);
_destHeight = (int)(sourceHeight * nPercent);
IntPtr inp1 = new IntPtr(0);
System.Drawing.Image thumb1 = originalImage.GetThumbnailImage(_destWidth, _destHeight, null, System.IntPtr.Zero);
originalImage.Dispose();
return thumb1;
}

//}
//catch (Exception ex)
//{
// string msg = ex.Message.ToString();
// return thumb1;
//}
}
#endregion

#region Resize Image
///
/// To resize image to a size
///

/// Input stream of the image.
/// Maximum Height to set
/// Maximum Width to set
/// Resize Type for the Image Hight, Width or Both
/// System.Drawing.Image ResizedImage
public static System.Drawing.Image ResizeImage(Stream InputStream, int? maxWidth, int? maxHeight, ImageClassType IType)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromStream(InputStream);
return ResizeImage(originalImage, maxWidth, maxHeight, IType);
}
///
/// To resize image to a size
///

/// Physical Path of the image.
/// Maximum Height to set
/// Maximum Width to set
/// Resize Type for the Image Hight, Width or Both
/// System.Drawing.Image ResizedImage
public static System.Drawing.Image ResizeImage(string FilePath, int? maxWidth, int? maxHeight, ImageClassType IType)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(FilePath);
return ResizeImage(originalImage, maxWidth, maxHeight, IType);
}
///
/// To resize image to a size
///

/// Original Image to resize.
/// Maximum Height to set
/// Maximum Width to set
/// Resize Type for the Image Hight, Width or Both
/// System.Drawing.Image ResizedImage
public static System.Drawing.Image ResizeImage(System.Drawing.Image originalImage, int? maxWidth, int? maxHeight, ImageClassType IType)
{
int sourceWidth = originalImage.Width;
int sourceHeight = originalImage.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
if (IType == ImageClassType.FixBoth)
{
if (maxWidth == null || maxHeight == null)
{
throw new Exception("Maximum height and maximum width are required to fix both.");
}
else
{
if (sourceWidth <= maxWidth && sourceHeight <= maxHeight)
{
return originalImage;
}
nPercentW = ((float)maxWidth / (float)sourceWidth);
nPercentH = ((float)maxHeight / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
}
else
{
nPercent = nPercentW;
}
}
}
else if (IType == ImageClassType.FixHeight)
{
if (maxHeight == null)
{
throw new Exception("Maximum height is required to fix height.");
}
else
{
if (sourceHeight > maxHeight)
nPercentH = ((float)maxHeight / (float)sourceHeight);
else
return originalImage;
nPercent = nPercentH;
}
}
else if (IType == ImageClassType.FixWidth)
{
if (maxWidth == null)
{
throw new Exception("Maximum width is required to fix width.");
}
else
{
if (sourceWidth > maxWidth)
nPercentW = ((float)maxWidth / (float)sourceWidth);
else
return originalImage;
nPercent = nPercentW;
}
}


/* for photo Thumb */
int DestinationWidth = (int)(sourceWidth * nPercent);
int DestinationHeight = (int)(sourceHeight * nPercent);
IntPtr inp1 = new IntPtr(0);
System.Drawing.Image thumb1 = originalImage.GetThumbnailImage(DestinationWidth, DestinationHeight, null, System.IntPtr.Zero);
originalImage.Dispose();
return thumb1;
}
///
/// To resize image to a size
///

/// Input stream of the image.
/// Maximum Height to set
/// Maximum Width to set
/// Resize Type for the Image Hight, Width or Both
public static void ResizeImage(Stream InputStream, int? maxWidth, int? maxHeight, ImageClassType IType, out int DestinationWidth, out int DestinationHeight)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromStream(InputStream);
ResizeImage(originalImage, maxWidth, maxHeight, IType, out DestinationWidth, out DestinationHeight);
}
///
/// To resize image to a size
///

/// Physical Path of the image.
/// Maximum Height to set
/// Maximum Width to set
/// Resize Type for the Image Hight, Width or Both
public static void ResizeImage(string FilePath, int? maxWidth, int? maxHeight, ImageClassType IType, out int DestinationWidth, out int DestinationHeight)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(FilePath);
ResizeImage(originalImage, maxWidth, maxHeight, IType, out DestinationWidth, out DestinationHeight);
}
///
/// To resize image to a size
///

/// Original Image to resize.
/// Maximum Height to set
/// Maximum Width to set
/// Resize Type for the Image Hight, Width or Both
public static void ResizeImage(System.Drawing.Image originalImage, int? maxWidth, int? maxHeight, ImageClassType IType, out int DestinationWidth, out int DestinationHeight)
{
int sourceWidth = originalImage.Width;
int sourceHeight = originalImage.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
if (IType == ImageClassType.FixBoth)
{
if (maxWidth == null || maxHeight == null)
{
throw new Exception("Maximum height and maximum width are required to fix both.");
}
else
{
if (sourceWidth <= maxWidth && sourceHeight <= maxHeight)
{
DestinationHeight = sourceHeight;
DestinationWidth = sourceWidth;
return;
}
nPercentW = ((float)maxWidth / (float)sourceWidth);
nPercentH = ((float)maxHeight / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
}
else
{
nPercent = nPercentW;
}
}
}
else if (IType == ImageClassType.FixHeight)
{
if (maxHeight == null)
{
throw new Exception("Maximum height is required to fix height.");
}
else
{
if (sourceHeight > maxHeight)
nPercentH = ((float)maxHeight / (float)sourceHeight);
else
nPercentH = 1;
nPercent = nPercentH;
}
}
else if (IType == ImageClassType.FixWidth)
{
if (maxWidth == null)
{
throw new Exception("Maximum width is required to fix width.");
}
else
{
if (sourceWidth > maxWidth)
nPercentW = ((float)maxWidth / (float)sourceWidth);
else
nPercentW = 1;
nPercent = nPercentW;
}
}


/* for photo Thumb */
DestinationWidth = (int)(sourceWidth * nPercent);
DestinationHeight = (int)(sourceHeight * nPercent);

}
#endregion

#region Resize Image Size get Destination hight and width
public void ResizeImageSize()
{
//try
//{
System.Drawing.Image originalImage = _InputImage;
int sourceWidth = originalImage.Width;
int sourceHeight = originalImage.Height;
int destX = 0;
int destY = 0;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;

/* for photo Thumb */
nPercentW = ((float)_Width / (float)sourceWidth);
nPercentH = ((float)_Height / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = (int)((_Height - (sourceWidth * nPercent)) / 2);
}
else
{
nPercent = nPercentW;
destY = (int)((_Width - (sourceHeight * nPercent)) / 2);
}
_destWidth = (int)(sourceWidth * nPercent);
_destHeight = (int)(sourceHeight * nPercent);

}
#endregion

#region ImageClassType
public enum ImageClassType
{
FixBoth,
FixHeight,
FixWidth
}
#endregion
}
}



=====================




======================================

No comments: