Reputation: 829
Uri myuri = new Uri("https://buffalousercontent.blob.core.windows.net/371-static/profileimages/full/39398");
This is my uri, when u hit means it receives image based on user some user doesn't contains image at that time it receives error page.
How to check the received one is image or something in C# code?
Upvotes: 0
Views: 1273
Reputation: 38079
You can use the Azure client API to do this. Add a reference to Microsoft.WindowsAzure.StorageClient
:
bool IsBlobJpeg(string blobUri)
{
try
{
Microsoft.WindowsAzure.StorageClient.CloudBlob blob =
new CloudBlob(
blobUri);
blob.FetchAttributes();
return (blob.Properties.ContentType == "image/jpeg");
}
catch (StorageClientException ex)
{
Console.WriteLine(String.Format("{0} Does not exist", blobUri));
return false;
}
}
Upvotes: 0
Reputation: 866
As mentioned in the other answers, you can look at the content type of the response, or you could try to create an image from the URI.
try {
var client = new WebClient();
var image = Image.FromStream(client.OpenRead(uri));
}
catch(ArguementException e) {
// this exception will be thrown if the URI doesn't point to a valid image.
}
Upvotes: 1
Reputation: 28097
Well a Uri
object itself doesn't know what will happen when you use it as the address of a GET request.
To check if a string
is a valid Uri
, you can use
Uri uri;
bool valid = Uri.TryCreate(stringInput, UriKind.Absolute, out uri);
To check what the file type of a request is
using (var client = new HttpClient())
{
var response = client.GetAsync(uri).Result;
var contentType = response.Content.Headers.ContentType;
}
Upvotes: 0
Reputation: 3338
Do a httprequest and check if the response contenttype equals "image/". This function should do the trick!
Boolean IsImageUrl(string URL)
{
var req = (HttpWebRequest)HttpWebRequest.Create(URL);
req.Method = "HEAD";
using (var resp = req.GetResponse())
{
return resp.ContentType.ToLower(CultureInfo.InvariantCulture)
.StartsWith("image/");
}
}
Upvotes: 0
Reputation: 126042
If you're just looking to validate that the URL contains an image:
bool ValidateImage(string url)
{
HttpWebRequest r = (HttpWebRequest)WebRequest.Create(url);
r.Method = "GET";
try
{
HttpWebResponse resp = (HttpWebResponse)r.GetResponse();
if (resp.ContentType == "image/jpeg")
{
Console.WriteLine("Image retrieved successfully.");
// Process image
return true;
}
else
{
Console.WriteLine("Unable to retrieve image");
}
}
catch
{
Console.WriteLine("Unable to retrieve image.");
}
return false;
}
Obviously, change the ContentType
check to whatever makes sense for your application. You could also use HEAD
as the request method (as in @Chris' answer) if you're just looking to validate the content type and not download the entire image right then.
Upvotes: 2
Reputation: 3713
To check if there is an error, you have to send a request and then receive an error or a good blob.
To send a request, you can use WebClient
as described here :
public bool CheckUri(Uri uri){
using(var client = new WebClient()){
try{
client.DownloadFile(uri);
return true;
}catch{//error detected
return false;
}
}
}
Upvotes: 0