0

I am trying to get all image files from a site, and show these in a gallery. So people can scroll through all the images like I did here with localy stored images.

After some searching I found this example but it was only meant for single image loading, and not for all images within this online directory, using the following.

//Location of image directory with xxx images.  
string directoryUrl = "http://www.foo.com/bar"; 

void DownloadTextures(string url)
{
    try
    {
        WebRequest req = WebRequest.Create(url);
        WebResponse response = req.GetResponse();
        Stream stream = response.GetResponseStream();
    }
    catch ( Exception e)
    {
        print("stuff went wrong: " +e)
    }
}

So how can I get all images out of online directory, and is this the right approach to get that done?

ps. It is all done in the unity engine, which has some limitations.

MX D
  • 2,453
  • 4
  • 35
  • 47
  • If you really just want to use all the images from a specific site then you go through the source code of the site and search for img tags and other elements where images can be and use those links to download them and display them – Vajura Oct 09 '14 at 09:43
  • @Vajura How would I aproach that? for example in [this scenario](http://gyazo.com/080be064a2e513891731bed1baf2fd9d) I got the link melloniax.com/images which contains several different images which I would like to import. – MX D Oct 09 '14 at 09:47
  • Oh such a structure should be failry easy can you give me the source code of this site? Paste it in a pastebin – Vajura Oct 09 '14 at 09:48
  • @Vajura http://pastebin.com/hk13XLXB is the css that runs behind it, finally I would like to reach a scenario where images are just online on a site/image folder. And get fetched from there, and either streamed to the gallery in c# (unity) or saved to the local machine and then shown. Preferably without having to be attached to web scripts – MX D Oct 09 '14 at 09:53
  • If you have all your images linked in a css like that and you can access that css then you can do it like this yes give me a sec gonna write it to show you. Also can you give me the direct link to this css? – Vajura Oct 09 '14 at 09:58
  • @Vajura http://www.melloniax.com/css/style.css – MX D Oct 09 '14 at 10:01

2 Answers2

1
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
private void Form1_Load(object sender, EventArgs e)
{
    string urlAddress = "http://melloniax.com/css/style.css";
    string urlBase = "http://melloniax.com";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    string data = "";
    if (response.StatusCode == HttpStatusCode.OK)
    {
        Stream receiveStream = response.GetResponseStream();
        StreamReader readStream = null;
        if (response.CharacterSet == null)
            readStream = new StreamReader(receiveStream);
        else
            readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
        data = readStream.ReadToEnd();
        response.Close();
        readStream.Close();
    }
    MatchCollection matches = Regex.Matches(data, @"\(..(?<link>[^.]*(\.jpg|\.png|\.gif)) *\)");
    for (int a = 0; a < matches.Count; a++)
        MessageBox.Show(urlBase + matches[a].Groups["link"].Value);
}

This works for your example, if you need anything else like how to actualy save the imgs from the url you got tell me.

Vajura
  • 1,112
  • 7
  • 16
  • So would it also be possible to obtain those images without having to consult the .css? – MX D Oct 09 '14 at 10:13
  • Yes if all those images are linked in the HTML like this **[^"]*)"** now you get in the results those links **"images/IMG-20131110-WA0005.jpg"** which you again just need to paste together with the base url to get to the img. You can test it here http://regex101.com/r/cM0sD7/1 . If you want to directly just access all the imges then you application needs permision to access /images then again you just parse through there and get the links – Vajura Oct 09 '14 at 10:18
  • That means the images should be referred either in the CSS or in one of the html files then if I understand correctly. And what I am trying to achieve is without them having to be linked to a html or css. Just images that are stored online, not necessarily used. – MX D Oct 09 '14 at 10:22
  • Yea i understood that just a minute ago and edited my post. Yes you can do that but as i said i cant access http://melloniax.com/images/ since i dont have permission if your application or your PC has permission to access it then you can do that. For a lazy solution just make a subpage on your site with all the images on it and then use code like this to parse it – Vajura Oct 09 '14 at 10:26
1

As regular expressions are really bad way to parse html, I believe it's better to use some library for your purposes. The best one I heard about is HtmlAgilityPack

The example how to parse images using it you can find here

Then you can use WebClient.DownloadFile method to download images like this. Please, note that images links could be relative, so you need to handle it too.

Community
  • 1
  • 1
Pj_pavel
  • 387
  • 3
  • 16