Friday, January 9, 2009

uplaoding an image and creating Thumbnail of same image according to heigth and width

Hi
in this article we create a class for uplaoding an image and creating
Thumbnail of same image according to heigth and width of orignal image
means if we uplaod a landscapte image then its thumbnail will genrate as
landscape if image is as potrate its thumbnail will genrate as portrare.


call this function

thumbnail oANB = new thumbnail();
oANB.CreateThumb(FileUpload1, @"Images\", @"Images\Thumbnail\");
oANB.CreateThumb(FileUpload1, @"Images\");


create a class with following code.


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using Microsoft.Win32;
using PAB.WebControls;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

public class thumbnail
{

public string UploadImage(string filename, string filetype, string str_folder_name_main, string str_folder_name_small,

FileUpload fileupload1, int width, int heigh)
{

if (fileupload1.PostedFile.ContentType == "image/pjpeg" || fileupload1.PostedFile.ContentType == "image/jpeg" ||

fileupload1.PostedFile.ContentType == "image/bmp" || fileupload1.PostedFile.ContentType == "image/gif")
{

string typ = fileupload1.PostedFile.ContentType;

fileupload1.PostedFile.SaveAs( HttpContext.Current.Server.MapPath( str_folder_name_main + filename + filetype));

if (str_folder_name_small.CompareTo(string.Empty) != 0)
{

CreateThumbnail(HttpContext.Current.Server.MapPath(str_folder_name_main + filename + filetype), width, heigh,

HttpContext.Current.Server.MapPath(str_folder_name_small + filename + filetype), 25, 25);
}

return "1";
}
else
{
return "0";
}
}

public string CreateThumbnail(string lcFilename, int lnWidth, int lnHeight, string OutputFilename, int hightPercent, int

widthPercent)
{
try
{
System.Drawing.Bitmap bmpOut = null;
Bitmap loBMP = new Bitmap(lcFilename);
ImageFormat loFormat = loBMP.RawFormat;
System.Drawing.Imaging.EncoderParameters Params = new System.Drawing.Imaging.EncoderParameters(1);
Params.Param[0] = new EncoderParameter(Encoder.Quality, 100L);
decimal lnRatio;
int thumbnamilHigth = Convert.ToInt32((loBMP.Height * hightPercent) / 100);
int thumbnamilWidth = Convert.ToInt32((loBMP.Width * widthPercent) / 100);

bmpOut = new Bitmap(thumbnamilHigth, thumbnamilWidth);

Graphics g = Graphics.FromImage(bmpOut);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.White, 0, 0, thumbnamilHigth, thumbnamilWidth);
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;

g.DrawImage(loBMP, 0, 0, thumbnamilHigth, thumbnamilWidth);
loBMP.Dispose();

bmpOut.Save(OutputFilename, ImageFormat.Jpeg);
bmpOut.Dispose();

return OutputFilename;
}
catch(Exception Ex)
{
return Ex.Message.ToString();

}


}

public bool checkImageType(HtmlInputFile FileUpload1)
{
if (FileUpload1.PostedFile.ContentType == "image/pjpeg" || FileUpload1.PostedFile.ContentType == "image/jpeg" ||

FileUpload1.PostedFile.ContentType == "image/bmp" || FileUpload1.PostedFile.ContentType == "image/gif")
{
return true;
}
else
{
return false;
}
}

public string CreateThumb(FileUpload fileUpload, string actIm, string tmbIm)
{
try
{
string filename = string.Empty;
string imagename = string.Empty;

string imgtype = string.Empty;
thumbnail thumb = new thumbnail();
filename = fileUpload.PostedFile.FileName;

imagename = filename.Substring(filename.LastIndexOf("\\") + 1, (filename.LastIndexOf(".") - 1) -

(filename.LastIndexOf("\\")));
imagename = imagename + DateTime.Now.Ticks.ToString() + GenrateRandomIamgeName(3);
imgtype = filename.Substring(filename.LastIndexOf("."));

if (thumb.UploadImage(imagename, imgtype, actIm, tmbIm, fileUpload, 95, 143).Equals("1"))
{
return imagename + imgtype;
}
else
{
return "";
}
}
catch
{
return "";
}


}

public string CreateThumb(FileUpload fileUpload, string actIm)
{
try
{
string tmbIm = string.Empty;
string filename = string.Empty;
string imagename = string.Empty;

string imgtype = string.Empty;
thumbnail thumb = new thumbnail();
filename = fileUpload.PostedFile.FileName;

imagename = filename.Substring(filename.LastIndexOf("\\") + 1, (filename.LastIndexOf(".") - 1) -

(filename.LastIndexOf("\\")));
imagename = imagename + DateTime.Now.Ticks.ToString() + GenrateRandomIamgeName(3);
imgtype = filename.Substring(filename.LastIndexOf("."));

if (thumb.UploadImage(imagename, imgtype, actIm, tmbIm, fileUpload, 95, 143).Equals("1"))
{
return imagename + imgtype;
}
else
{
return "";
}
}
catch
{
return "";
}


}



public string GenrateRandomIamgeName(int Length)
{
string listOfCharacter = "abcdefghijkmnopqrstuvwxyz0123456789";
Random randNum = new Random();
char[] chars = new char[Length];
int allCharacterCount = listOfCharacter.Length;

for (int i = 0; i < Length; i++)
{
chars[i] = listOfCharacter[(int)((listOfCharacter.Length) * randNum.NextDouble())];
}

return new string(chars);
}


}


Thanks
HelpOnDesk Team

No comments: