Saturday, May 26, 2012

Get Images from folder and generate HTML anchor in asp.net

Here i will show how to get the image from folder then create a anchor link to that image and assigned to string builder "sb".."sPhysicalPath" is the path of gallery folder.The class Directory Info is used to get the all the image files from the folder
public static string BindImages(int images, bool isRedirecting)
{
string thumbNailFolderName = ConfigurationManager.AppSettings["ThumbFolder"];
string previewFolderName = ConfigurationManager.AppSettings["PreviewFolder"];
StringBuilder sb = new StringBuilder("");
string format = "<span><a href=\"{0}\" title=\"\"><img alt=\"\" src=\"{1}\" /></a></span>";string sPhysicalPath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["GalleryFolder"]);
DirectoryInfo oDir = new DirectoryInfo(sPhysicalPath);
FileInfo[] oFiles = oDir.GetFiles();
int i = 0;
foreach (FileInfo oFile in oFiles)
{
i++;
sb.AppendFormat(format, (isRedirecting ? "Gallery.aspx" : "school/gallery/" + previewFolderName + "/" + oFile.Name),
"school/gallery/" + thumbNailFolderName + "/" + oFile.Name);
if (i == images && images != 0)
break;
}
sb.Append("");
return sb.ToString();
}

No comments:

Bel