-1

i try want to detect if an image exists but can't get it to work. If need to load the image if it exits and if not exits it needs to load a default image. The IF/ELSE is working just the first part does not, what am i doing wrong i get all the time the default image

@{ String imageURL = "http://www.XXXX.com/cdcover.jpg')"; 
}
@if (File.Exists(imageURL)) {
    @section cdcover{ src="http://www.XXXX.com/cd/XX/cdcover.jpg")" }
    }
else { 
    @section cdcover{ src="http://www.XXXX.com/cd/defaulcover.jpg" }   
    }
Ewald Bos
  • 1,560
  • 1
  • 20
  • 33
  • 1
    http://stackoverflow.com/a/980910/28004 – balexandre May 29 '16 at 16:42
  • 1
    Possible duplicate of [Inputting a default image in case the src arribute of an html is not valid?](http://stackoverflow.com/questions/980855/inputting-a-default-image-in-case-the-src-arribute-of-an-html-img-is-not-valid) – balexandre May 29 '16 at 16:42
  • Yes i was searching all the time for a solution and had not found this page, thanks balexandere, the onerror="if (this.src != 'error.jpg') this.src = worked perfect for my solution – Ewald Bos May 29 '16 at 16:51

1 Answers1

0

Here is a concept:

var bitmapImage = new BitmapImage(); // An image var
bitmapImage.BeginInit();
bitmapImage.UriSource = new Uri("/*image url*/");; // set a value to our var
bitmapImage.EndInit(); img.Source = bitmapImage;

if (File.Exists(bitmapImage)){ // checks if the var has an existing image
//code

}
ZakTheHat
  • 1
  • 5