Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1910
Display images in a WebImageViewer from a folder
posted

Can someone provide an example of how to display all images from a specific folder in the WebImageViewer?

Parents
  • 6365
    Offline posted

    Hello Angela,

    The WebImageViewer exposes an Items collection that can be populated with ImageItem objects (a single object represents a single image). We can manually create these objects by passing the relative image path in their constructor. We can do this for all images from a specific folder by using the built-in .NET API.

    For example:

    protected void Page_Load(object sender, EventArgs e)
    {
    PopulateImageViewer();
    }
    
    private string[] GetFiles()
    { 
    string webCurrentDirectory = HttpRuntime.AppDomainAppPath;
    string[] filePaths = Directory.GetFiles(webCurrentDirectory + @"\images\", "*.jpg", SearchOption.TopDirectoryOnly);
    string[] fileNames = filePaths.Select(filePath => Path.GetFileName(filePath)).ToArray();
    return fileNames;
    }
    
    private void PopulateImageViewer()
    {
    string[] fileNames = GetFiles();
    
    foreach (string fileName in fileNames)
    {
    ImageItem imageItem = new ImageItem(@"images/" + fileName, fileName, fileName + " tooltip");
    imageViewer.Items.Add(imageItem);
    }
    }

    Please note that the above code is just a single approach that can be used and in order to work with extensions other than .jpg, we can simply use the Directory.GetFiles method again by changing the searchPattern argument.

    I have attached a sample application that demonstrates the mentioned approach.

    If you have any questions, please let me know.

    5758.WebImageViewer_sample.zip

Reply Children
No Data