Software Programs and Projects

This Software Programs and Projects include programing tips and technology using different languages like VC++,ASP,JSP C#.Net,PHP,VB.Net,JavaScript,ASP.NET .Software Programs and Projects blog mainly support to software programmers and provide help through the mail.

How To Create Thumnail in Asp.NET with C#.NET

//Return thumbnail callback
public bool ThumbnailCallback()
{
return true;
}


///For Image Thumbnail

fileUploadImage : FileUpload Control

m_strRealImagePath : Real Image Path

m_strThumbnailImagePath : Thumbnail Image Path

lblError1 : Label Control



private void MakeThumbnail(string strProductImageName)
{

System.Drawing.Image myThumbnail150;
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);

if (fileUploadImage.PostedFile != null && fileUploadImage.PostedFile.ContentLength > 0)
{
//this code used to remove some symbols between image name and replace with space
string strFileName = fileUploadImage.PostedFile.FileName.Replace('%', ' ').Substring(fileUploadImage.PostedFile.FileName.LastIndexOf("\\") + 1);

string strExtention = System.IO.Path.GetExtension(strFileName);
string imgname1 = strProductImageName.ToString();
string imgname2 = imgname1.Replace('#', ' ').Substring(imgname1.LastIndexOf("\\") + 1);
string imgname3 = imgname2.Replace('@', ' ').Substring(imgname1.LastIndexOf("\\") + 1);
string imgname4 = imgname3.Replace(',', ' ').Substring(imgname1.LastIndexOf("\\") + 1);
string imgname5 = imgname4.Replace('&', ' ').Substring(imgname1.LastIndexOf("\\") + 1);

Finalimagename = imgname5.ToString() + strExtention.ToString();

string imgname = fileUploadImage.PostedFile.FileName.Substring(fileUploadImage.PostedFile.FileName.LastIndexOf("\\") + 1);

string sExtension = imgname.Substring(imgname.LastIndexOf(".") + 1);

///Check File Exists Or Not

if (File.Exists(m_strRealImagePath + Finalimagename))
{
File.Delete(m_strRealImagePath + Finalimagename);
}

//this code is used to check image extension

if (sExtension.ToLower() == "jpg" || sExtension.ToLower() == "gif" || sExtension.ToLower() == "bmp" || sExtension.ToLower() == "jpeg")
{
if (!File.Exists(m_strRealImagePath + Finalimagename))
{
fileUploadImage.PostedFile.SaveAs(ResolveUrl(m_strRealImagePath + Finalimagename));

System.Drawing.Image imagesize = System.Drawing.Image.FromFile(ResolveUrl(m_strRealImagePath + Finalimagename));
Bitmap bitmapNew = new Bitmap(imagesize);
if (imagesize.Width < imagesize.Height)
{
myThumbnail150 = bitmapNew.GetThumbnailImage(150 * imagesize.Width / imagesize.Height, 150, myCallback, IntPtr.Zero);
}
else
{ myThumbnail150 = bitmapNew.GetThumbnailImage(150, imagesize.Height * 150 / imagesize.Width, myCallback, IntPtr.Zero);
}

//Save image in TumbnailImage folder

if (File.Exists(m_strThumbnailImagePath + Finalimagename))
{
File.Delete(m_strThumbnailImagePath + Finalimagename);
}
myThumbnail150.Save(ResolveUrl(m_strThumbnailImagePath) + Finalimagename, System.Drawing.Imaging.ImageFormat.Jpeg);
lblError1.Text = "Successfully uploaded";
imgProductImage.Visible = true;
m_strProductThumbnailImagePath = "~/UserData/ThumbnailImage/"+ Finalimagename;; m_strProductRealImagePath = "~/UserData/RealImage/"+ Finalimagename;
string imgDisplayName = m_strProductThumbnailImagePath;
imgProductImage.ImageUrl = imgDisplayName;
}
}
else
{
lblError1.Visible = true; lblError1.Text = "Check image extension";
}
}
} tag:-How To create thumbnail in asp.net with c#.net,

0 comments:

Post a Comment

Post a Comment