Solved

check if image exists

Posted on 2007-11-22
7
3,835 Views
Last Modified: 2013-12-16
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
Comment
Question by:ANAT2403
7 Comments
 
LVL 10

Expert Comment

by:joex911
ID: 20335088
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
 
LVL 11

Expert Comment

by:cmhunty
ID: 20335119
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
 

Author Comment

by:ANAT2403
ID: 20335211
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 11

Expert Comment

by:cmhunty
ID: 20335389
Use System.Net. With my limited knowledge of C#, I tried to recreate but struggled. Maybe someone else could convert?
0
 
LVL 10

Accepted Solution

by:
joex911 earned 250 total points
ID: 20335466
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
 
LVL 11

Assisted Solution

by:cmhunty
cmhunty earned 250 total points
ID: 20335512
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
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20335663
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now