• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3847
  • Last Modified:

check if image exists

In ASP.NET 2.0 with C# I have a page with an <asp:Image> control.
I have the img.ImageUrl that contain the link for the image.
I want to make a check if the image exists. How do I do this check?
thankyou
Anat
0
ANAT2403
Asked:
ANAT2403
2 Solutions
 
Oliver AmayaEntrepeneurCommented:
Hi, this is one way of doing it:
// for relative URL
bool imgFound = System.IO.File.Exists(Server.MapPath(img.ImageUrl));
 
// for absolute URL
bool imgFound = System.IO.File.Exists(img.ImageUrl);

Open in new window

0
 
cmhuntyCommented:
If the URL is not local, you can use something like this (sorry, it's in VB.NET but may give you an idea):

    Private Function ImageExists(ByVal strURL As String) As Boolean
        Dim blExists As Boolean = True
        Try
            Dim req As HttpWebRequest = WebRequest.Create(strURL)
            Dim res As HttpWebResponse = req.GetResponse
        Catch ex As Exception
            blExists = False
        End Try
        Return blExists
    End Function
0
 
ANAT2403Author Commented:
Hi joex911
this is my imgurl:
http://192.168.1.101/appsrv/vg_paint?s=main&w=110&bg=bg1&h=80&q=100&f=models/79942.jpg
this return an image
and this does not return anything:
http://192.168.1.101/appsrv/vg_paint?s=main&w=110&bg=bg1&h=80&q=100&f=models/5.jpg
your first suggestion for relative URL returned an error on both cases and you second returned false for both cases. Do you have another suggestion?
Hi cmhunty
I could not define HttpWebRequest . What using do I need for this?
Thankyou
Anat

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
cmhuntyCommented:
Use System.Net. With my limited knowledge of C#, I tried to recreate but struggled. Maybe someone else could convert?
0
 
Oliver AmayaEntrepeneurCommented:
With the method's I put up the image has to be local on the server. I can convert cmhunty code to c# though
private bool ImageExists(string strURL)
{
  bool blExists = true
  try
  {
    System.Net.HttpWebRequest req = System.Net.WebRequest.Create(strURL);
    System.Net.HttpWebResponse res = req.GetResponse();
  }
  Catch(Exception ex)
  {
    blExists = false;
  }
  return blExists;
}

Open in new window

0
 
cmhuntyCommented:
Managed to convert to C# - seems you can't convert HttpWebRequest to WebRequest in C# but can in VB.NET

private bool ImageExists(string strURL)
{
  bool blExists = true;
  try
  {
      WebRequest req = WebRequest.Create(strURL);
      WebResponse res = req.GetResponse();

  }
  catch(Exception ex)
  {
    blExists = false;
  }
  return blExists;
}
0
 
Göran AnderssonCommented:
VB.NET allows an implicit conversion, but only if you don't use Option Strict.

In C# you always have to do an explicit cast if the compiler can't tell that the cast is safe:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strURL);
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now