views:

694

answers:

2

Instead of populating my Repeater with pictures from a folder in my project i want to polulate it with a link to where the image is like so..............

http://www.erate.co.za/imgGrab.aspx?Id=99

What code changes must i make to let my code look at my hyperlink for the image and not the "pics" folder?

My Page Load

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string sBasePath = System.Web.HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"];
            if ( sBasePath.EndsWith("\\"))
                sBasePath = sBasePath.Substring(0,sBasePath.Length-1);

            sBasePath = sBasePath + "\\" + "pics";

            System.Collections.Generic.List<string> oList = new System.Collections.Generic.List<string>();
            foreach (string s in System.IO.Directory.GetFiles(sBasePath, "MyPicture.jpg"))
            {
                //We could do some filtering for example only adding .jpg or something
                oList.Add( System.IO.Path.GetFileName( s ));

            }
            repImages.DataSource = oList;
            repImages.DataBind();
        }

    }

My ItemDataBound event for my Repeater (called repImages)

 protected void repImages_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
            e.Item.ItemType == ListItemType.Item)
        {
            string sFile = e.Item.DataItem as string;

            //Create the thumblink
            HyperLink hlWhat = e.Item.FindControl("hlWhat") as HyperLink;
            hlWhat.NavigateUrl = ResolveUrl("~/pics/" + sFile  );
            hlWhat.ToolTip = System.IO.Path.GetFileNameWithoutExtension(sFile);
            hlWhat.Attributes["rel"] = "imagebox-bw";

            Image oImg = e.Item.FindControl("imgTheImage") as Image;
            oImg.ImageUrl = ResolveUrl("~/createthumb.ashx?gu=/pics/" + sFile + "&xmax=100&ymax=100" );


        }

    }

Thanks in advanced! Etienne

A: 

In the repeater's template include an <img> tag. If you need to programmatically set the source, make it runat="server".

Richard
Thanks, but how would i integrate it with my current code?
Etienne
@Richard, the Repeater already contains an asp:Image control.
Cerebrus
@Etienne: Don't know without seeing the markup (likely the answer is to use data binding).
Richard
A: 

If I understand it correctly, this is a multi-faceted problem :

a. You wish to load images from an internet URL instead of a local folder. This means that you have to have some way of listing the files available at the remote URL. If you look at the problem objectively, you will realize that the only difference between retrieving images from a remote location and from your local system is that the files are easily enumerable using the DirectoryInfo class. The GetFiles method here returns an array of FileInfo and you can get the actual filename for each using the FileName property. So basically, you need a list of strings that map to the location of the file.

b. Enumerating the files from a remote location alone depends on many factors such as the protocol you are using to retrieve that list. If you're using HTTP, and we can assume that you have some control over the server at that location, then the URL would need to primarily support directory browsing. You will then need to parse that served directory list (which is non-standard and server specific) and load all the available image URL's as strings. You can then populate your Repeater with that list. See this discussion for a possible solution.

If the server supports FTP, then you work becomes easier because you can enumerate the images using the WebRequestMethods.Ftp.ListDirectory or WebRequestMethods.Ftp.ListDirectory methods.

c. If you want to create thumbnails for the images, you will have to write code to download each image, save it in a temporary location and then perform dimensional manipulations upon it.

d. If however, the list of images available at the internet location is static and you already have that information, you can simply load that list as a list of strings and set each Image control's src property to that list. The thumbnails will however remain a problem, unless you can upload your Thumbnail creator (the HTTP handler) at that URL too, so that files are retrieved locally for manipulation and thumbnails are served to you automatically.

Cerebrus
Thanks for this - any code examples please to go with your explanation?
Etienne
Well, that's difficult to do until you answer some of the questions/possibilities I have mentioned. I think this question is more of "Should I..." rather than "How to...?"
Cerebrus
MMM, maybe i must say save all my pictures in a folder. It just at the moment they are saved in my DB and i did not want to change my code.
Etienne
But your question asks about loading them from an internet URL!! If they are in a DB, then most of our discussion above becomes irrelevant because you can run a simple SQL query to populate your Repeater.
Cerebrus
I agree........but then it can still be a problem with the thumbnails?
Etienne
No, because you would be able to load the file bytes into an Image object and manipulate accordingly.
Cerebrus