Pages

Monday, July 6, 2009

Creating Thumbnail Images in C#

Hi All,

The image processing is a very typical task, but still it is simple to do. There are application where we need to create the thumbnail of the uploaded images, at that time we need to resize the image. here is the sample code through which we can resize the image easily..
We just need to use the 2-3 main name spaces that are

1. System.Drawing
2. System.Drawing.Imaging
3. System.Drawing.Drawing2D

with the use of these name spaces we can do our task. i have made a function by which we can create the thumbnail of the imgae, we just need to pass some parameter like image data in Byte, path to save the image, required width and the height of the image.

please see the sample function below..

using System;
using System.IO;
using System.Web;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Web.Security;
using System.Configuration;

///
/// Summary description for ImgUtility
///

public class ImgUtility
{
static string strFileName;
public ImgUtility()
{
//
// TODO: Add constructor logic here
//

}
const int sizeThumb = 69;

public static string uploadImage(string sPath, Stream data)
{
string dirName = string.Empty;
string newPath = string.Empty;
int length = Convert.ToInt32(data.Length);
byte[] origImageData = new byte[length];
data.
Read(origImageData, 0, length);
strFileName
= DateTime.Now.ToString("ddMMyyyy_HHmmss");
dirName
= Path.GetDirectoryName(sPath);
if (sPath.Contains("Avtaar") != true)
{
if (sPath.Contains("features") == true)
{
newPath
= Path.Combine(dirName, @"features/" + strFileName + ".jpg");
return FixedSize(origImageData, newPath, 200, 108);
}
else
{
newPath
= Path.Combine(dirName, @"Thumbnail/" + strFileName + ".jpg");
FixedSize
(origImageData, newPath, 120, 85);
newPath
= Path.Combine(dirName, @"Photo/" + strFileName + ".jpg");
FixedSize
(origImageData, newPath, 500, 375);

//return @"\MyDiscoverySchool\Thumbnail\"+strFileName + ".jpg";
return @"Thumbnail/" + strFileName + ".jpg";
//return strFileName + ".jpg";
}
}
else
{
newPath
= Path.Combine(dirName, strFileName + ".jpg");
return FixedSize(origImageData, newPath, 76, 94);
}
}

public static string FixedSize(byte[] fullsize, string sPath, int Width, int Height)
{
Image imgPhoto
;
imgPhoto
= Image.FromStream(new MemoryStream(fullsize));
int sourceWidth = imgPhoto.Width;
int sourceHeight = imgPhoto.Height;
int sourceX = 0;
int sourceY = 0;
int destX = 0;
int destY = 0;

float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;



nPercentW
= ((float)Width / (float)sourceWidth);
nPercentH
= ((float)Height / (float)sourceHeight);
if (nPercentH < nPercentW)
{
nPercent
= nPercentH;
destX
= System.Convert.ToInt16((Width - (sourceWidth * nPercent)) / 2);
}
else
{
nPercent
= nPercentW;
destY
= System.Convert.ToInt16((Height - (sourceHeight * nPercent)) / 2);
}

int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);

Bitmap bmPhoto
= new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
bmPhoto.
SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

Graphics grPhoto
= Graphics.FromImage(bmPhoto);
grPhoto.
Clear(Color.White);
grPhoto.
InterpolationMode = InterpolationMode.HighQualityBicubic;

grPhoto.
DrawImage(imgPhoto, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel);

grPhoto.
Dispose();
bmPhoto.
Save(sPath, System.Drawing.Imaging.ImageFormat.Jpeg);
//return bmPhoto;
return strFileName;
}
}

Happy Coding!!!!!!!!!! :)

Thanks
Anil Kumar Pandey
System Architect
Green Point Technology (India) Ltd.
Mumbai, Maharshtra
INDIA

No comments:

Post a Comment

Kontera