C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Images »

Creating Thumbnail Images in C#


Posted Date: 06 Jul 2009    Resource Type: Code Snippets    Category: Images
Author: Anil Kumar PandeyMember Level: Diamond    
Rating: 1 out of 5Points: 15






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


For more details, visit http://mannpandey.blogspot.com/2009/07/creating-thumbnail-images-in-c.html



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Thumbnail  .  Setting height and width  .  Image Processing  .  Image manipulation  .  Creating thumbnail  .  Anil pandey  .  Anil kumar pandey  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Retrieve images from database in a gridview
Previous Resource: Saving the image file in SQL server
Return to Discussion Resource Index
Post New Resource
Category: Images


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use