Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Detect Missing Image

Detect Missing Image

Scheduled Pinned Locked Moved ASP.NET
helpquestioncsharpasp-netsysadmin
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    richmondwill
    wrote on last edited by
    #1

    Hello All, I hope this is a question with a simple solution that I've overlooked. I am loading an image reference (URL) into a ASP.NET image control when the page loads. The problem is that I'm loading a reference from an MLS system and the image link is occasionally broken. Is there a simple way to reference a property of the image control to tell if the image sucessfully loaded? If so, I can't find it. Currently I'm using code to detect if the URL is valid before setting the property of the image control. This requires a round trip to the image host server to detect a 404 response. It works, but is SLOW when there are more than a dozen pictures. I hate slow... Any help is appreciated. Cheers, Will

    L M 2 Replies Last reply
    0
    • R richmondwill

      Hello All, I hope this is a question with a simple solution that I've overlooked. I am loading an image reference (URL) into a ASP.NET image control when the page loads. The problem is that I'm loading a reference from an MLS system and the image link is occasionally broken. Is there a simple way to reference a property of the image control to tell if the image sucessfully loaded? If so, I can't find it. Currently I'm using code to detect if the URL is valid before setting the property of the image control. This requires a round trip to the image host server to detect a 404 response. It works, but is SLOW when there are more than a dozen pictures. I hate slow... Any help is appreciated. Cheers, Will

      L Offline
      L Offline
      LeeOvEngland
      wrote on last edited by
      #2

      Hi, Have you tried wrapping it in a Try - Catch statement. If it fails to load then the code will just carry on and you can use a default missing image or code in the catch part to do something else.

      R 1 Reply Last reply
      0
      • L LeeOvEngland

        Hi, Have you tried wrapping it in a Try - Catch statement. If it fails to load then the code will just carry on and you can use a default missing image or code in the catch part to do something else.

        R Offline
        R Offline
        richmondwill
        wrote on last edited by
        #3

        LeeOvEngland, The statement is wrapped in a Try - Catch. It merrily continues on even though an invalid picture url is assigned to the Image object's ImageUrl property. It seems strange that no event is raised in this scenario. Thanks, Will

        1 Reply Last reply
        0
        • R richmondwill

          Hello All, I hope this is a question with a simple solution that I've overlooked. I am loading an image reference (URL) into a ASP.NET image control when the page loads. The problem is that I'm loading a reference from an MLS system and the image link is occasionally broken. Is there a simple way to reference a property of the image control to tell if the image sucessfully loaded? If so, I can't find it. Currently I'm using code to detect if the URL is valid before setting the property of the image control. This requires a round trip to the image host server to detect a 404 response. It works, but is SLOW when there are more than a dozen pictures. I hate slow... Any help is appreciated. Cheers, Will

          M Offline
          M Offline
          minhpc_bk
          wrote on last edited by
          #4

          Hi there, There are two simple ways come to mind: + Use client side script to detect a bronken link. As you already know that the image control is rendered as an img element at the client side. If the url specified in the src property is a broken link (missing image), the onerror event will be raised. So you basically can register an event handler for this event to set the default image in this case. The sample code in your web page is something like this:

          function Image_OnError(obj)
          {
          obj.src = "Images/defaultPhoto.jpg";
          }
          ...
          <asp:Image onerror="Image_OnError(this);" id="Image1"
          runat="server" ImageUrl="Images\myphoto.jpg"></asp:Image>

          + Use server side code to detect a broken link. You simply create a seperate web page which is resposible for detecting/loading the requested image, say ShowImage.aspx. In the Page_Load event, you just assign the ShowImage.aspx with the image url to the ImageUrl property of the image control:

          private void Page_Load(object sender, System.EventArgs e)
          {
          string url = "http://localhost/WebApplication1/Images/MyPhoto.jpg";
          url = Server.UrlEncode(url);
          Image1.ImageUrl = "ShowImage.aspx?url=" + url;
          ...
          }

          The sample code for the ShowImage.aspx goes like this:

          private void Page_Load(object sender, System.EventArgs e)
          {
          string url = Request.QueryString["url"];
          url = Server.UrlDecode(url);
          if(ImageExits(url)
          {
          LoadRequestedImage(url);
          }
          else
          {
          LoadDefaultImage();
          }
          }

          Perhaps, you may want to use the first option because of the performance as you are expecting.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups