Im using regex to pull an image url from a site and would like my site to display the image from that url.
Anyone have an idea on how to do this since setting the image url to the response url isn't working?

Dani AI

Generated

A few targeted points to clarify and test based on 's posts and 's suggestion.

The most likely immediate problem is that the value assigned to the image control is not a plain URL. An image element’s src must be a bare HTTP(S) URL (or data URI). If the constructed string contains HTML (for example an anchor tag taken from the source), the browser will not fetch the image. Inspect the rendered HTML (View Source / Inspect Element) and the network panel to see the final src and any HTTP status for that request.

If the parsing runs on the server before the page is rendered, setting the server control’s ImageUrl will work without a client reload. If the parse happens after rendering, the DOM must be updated. Practical approaches:

  • Client-side update (no full reload): expose the parsed URL (or a tiny server endpoint that returns it) and set the img src in JavaScript. Example pattern:

    <img id="poster" alt="poster">
    
    <script>
    fetch('/GetPosterUrl?id=tt1234567')
      .then(r => r.text())
      .then(url => { document.getElementById('poster').src = url; });
    </script>

    For WebForms server controls use the control’s client id, e.g. document.getElementById('<%= moviePicture.ClientID %>').

  • WebForms partial update: wrap the image in an UpdatePanel and set the ImageUrl on a partial postback.

Other recommendations and checks:

  • Prefer an HTML parser (Html Agility Pack) over brittle regex for reliable scraping: Html Agility Pack.
  • Verify the raw image URL directly in the browser to detect hotlink blocking or 403 responses. If the host blocks hotlinking, serve the image through a server-side proxy/handler.
  • Validate that the final URL is well-formed and starts with http:// or https://.

Troubleshoot by confirming the final src the browser sees, checking network errors, and ensuring the server-side string contains only the URL (no HTML).

Recommended Answers

All 3 Replies

If you want to display the image in the browser, I think, you should use an asp:control so you can reference it from the server side asp like this:

<body>
    <form runat="server">
        ...
        <asp:Image ID="Image1" runat="server" />
        ...
    </form>
</body>

To set the url use the following in the code behind:

Image1.ImageUrl = "//location/img.png";

Also I think there is a seperate forum for ASP/ASP.NET, you might find more able people to help you there.

I hope this helps!

First thanks for the reply and yeah someone was kind enough to move this message to the correct forum.
So far this is what i have tried:
(code behind)

<asp:Image ID="moviePicture" runat="server" Height="333px" Width="330px" />

(.cs code)

Regex imgLinkReg = new Regex(
                "name\\=\\\"poster\\\".*src\\=\\\"http\\:\\/\\/ia\\.media-imd" + "b\\.com\\/images\\/(.*?)\\\"",
                RegexOptions.CultureInvariant
                | RegexOptions.Compiled
                );
Match m1 = imgLinkReg.Match(responseFromServer);
if (m1.Success)
                {
                    moviePicture.ImageUrl = "http://ia.media-imdb.com/images/" + m1.Groups[1];
                }

--The Regex code was created using expresso and i have been able to put the right url to a label as a test. The imagebox (not a standalone box) starts out with a blank target url.
--I'm getting the impression that it needs to have the image reload after grabbing the correct url but I don't wish to reload the page to do it. Im thinking that i might want to use ajax but I'm hoping there is a simpler way to accomplish such a mundane task

As far as I understand, if you want to do this using C# you will have to reload at least part of the page. I do suspect there is a way of doing this using Javascript although I am no expert at web development.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.