Wednesday, October 15, 2008

Complete example of create Thumbnail when we uplaod image

how to create a thumnain while we are uploading a new image


Code in aspx page

protected void Button1_Click(object sender, EventArgs e)
{
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("\\")));
imgtype = filename.Substring(filename.LastIndexOf("."));

string str = thumb.UploadImage(imagename, imgtype, Server.MapPath(@"Images\Actual\"),

Server.MapPath(@"Images\Thumbnail\"), fileUpload,95,143);

}

code in HTML part :-

< asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" / >

< asp:FileUpload ID="fileUpload" runat="server" / >

Code in class file

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.Drawing;
using System.Drawing.Imaging;

///
/// Summary description for thumbnail
///

public class thumbnail
{
public thumbnail()
{
//
// TODO: Add constructor logic here
//
}


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")
{
fileupload1.PostedFile.SaveAs(str_folder_name_main + filename + filetype);
CreateThumbnail(str_folder_name_main + filename + filetype, width , heigh , str_folder_name_small + filename +

filetype);
// CreateThumbnail(str_folder_name_main + filename + filetype, 250, 300, str_folder_name_large + filename +

filetype);
return "1";
}
else
{
return "2";
}
}

public void CreateThumbnail(string lcFilename, int lnWidth, int lnHeight, string OutputFilename)
{
System.Drawing.Bitmap bmpOut = null;
Bitmap loBMP = new Bitmap(lcFilename);
ImageFormat loFormat = loBMP.RawFormat;
decimal lnRatio;
int lnNewWidth = 0;
int lnNewHeight = 0;
lnNewWidth = lnWidth;
lnNewHeight = lnHeight;
bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
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, lnNewWidth, lnNewHeight);
g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
loBMP.Dispose();

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


}

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;
}
}

}

Thanks
helpondesk Team

No comments: