I’m not used to work with remote resources, so maybe this question is trivial, but I’m not sure how to implement it.
I need to load a collection of images that are placed in a http folder, for example http://www.myexsamplesite.com/images.
Inside this directory I could have N images with different names.
I need to show these images in a WinForms application (in a ListView).
The alternative that I’m studying is downloading the images to a tmp dir, and then load them from disk from my WinForms application. I’m using the following:
WebClient fileReader = new WebClient();
fileReader.DownloadFile(imageAddress, filePath);
to download the files to disk. My question is:
How can I download all the files in the http folder? I guess that I need to enumerate the files in the folder and then download one to one.
The second question and most important. I’m sure that there is a much better mechanism to perform this task. So:
Is there any other way to get the http image content into a System.Drawing.Image?
Remember that I need to get all the files in the http folder.
Thanks in advance
First, to get all of the files, you need a way to enumerate them. HTTP does not have any method that lets you download a whole directory, or technically even list what’s in a directory. Some web servers are set to send you a pre-defined web page which displays the list of files in a directory when it lacks a default page, but not all are. You could parse that content to get a list of the files you need to download. Otherwise, you’ll need to find a way to get that info programmatically, based on the web application itself.
Second, you can load an
imagefrom astream, as I recall. Instead of usingWebClient, you can useHttpWebRequestso that you can access the content stream, and feed that into your constructor. However, I think it may be better/easier for you to use a temp directory and load the files from there. Sometimes unexpected things might happen when loading things from a network stream like this (for example, the stream of GIF images with animations is attempted to be kept open to continually read the frames of the animation), which could cause exceptions at odd times you might not be prepared to debug and understand off the bat.Finally, note that you would only be using
mime typehere to verify that your content is an image – so you explicitly would not load something that is mime typeapplication/octet-streaminto animageobject – but only things likeimage/jpeg,image/gif,image/pngetc.