dezkev
dezkev

Reputation: 599

Cannot Convert System.String to System.Uri

I am using the Web Client Class to download files from the internet (Flickr actually). This works fine as long as I use : WebClient().DownloadData(string) , however this locks up the UI as it is Not asynchronous.

However when I try WebClient().DownloadDatAsync(string), I get a compile error: "Unable to convert System.String to System.Uri".

The string MediumUrl returns "http://farm4.static.flickr.com/2232/2232/someimage.jpg"

So the question is how do I convert the string "http://farm4.static.flickr.com/2232/2232/someimage.jpg" to a Uri.

Things I have tried-

  1. I have tried to cast it to Uri but that does not work either.
  2. I have tried Uri myuri = new uri(string) - errors out as above.

    foreach (Photo photo in allphotos)  
    {  
        //Console.WriteLine(String.Format("photo title is :{0}", photo.Title));
        objimage = new MemoryStream(wc.DownloadData(photo.MediumUrl));
        images.Add(new Pictures(new Bitmap(objimage), photo.MediumUrl, photo.Title));  
    }
    

Upvotes: 23

Views: 97868

Answers (6)

lalaki
lalaki

Reputation: 1

Like this, time-consuming behaviour uses threads

ThreadPool.QueueUserWorkItem(_ =>
{
    const string url = "https://github.com/favicon.ico";
    using (var client = new WebClient())
    using (var ns = client.OpenRead(url))
    {
        // Get Network Image
        var image = Image.FromStream(ns);
        
        // if winform
        pictureBox1.Invoke((MethodInvoker)(() => pictureBox1.Image = image));
    }
});

Upvotes: 0

Peter Kelly
Peter Kelly

Reputation: 14401

Okay, so I think if the others have proved your URI is valid in their code and it compiles etc. and you also mention it is generated at runtime - it could be that the UriString you are generating at runtime is invalid and not what you are expecting?

Instead of letting an exception be thrown for attempting to create a Uri from an invalid string, I would suggest the following method IsWellFormedUriString on the Uri class.

string uriString = "your_UriString_here";

if (Uri.IsWellFormedUriString(uriString, UriKind.Absolute))
{
    Uri uri = new Uri(uriString);
}
else
{
    Logger.WriteEvent("invalid uriString: " + uriString);
}

Might help with your debugging also.

Upvotes: 8

Daniel Elliott
Daniel Elliott

Reputation: 22877

var yourUri = new UriBuilder(yourString).Uri;

So your example would be:

wc.DownloadDataAsync(new UriBuilder(photo.MediumUrl).Uri);
objimage = new MemoryStream(wc.Result);

You may need to put a check in to see the operation has completed.

Hope that helps,

Dan

Upvotes: 5

Digitalex
Digitalex

Reputation: 1494

This works just fine;

System.Uri uri = new System.Uri("http://farm4.static.flickr.com/2232/2232/someimage.jpg");

By the way; I notice you mistyped the expression new uri(..., with lowercase uri. This is not your problem, is it? Because it should be "new Uri".

Upvotes: 44

Mitch Wheat
Mitch Wheat

Reputation: 300739

If I understand your code correctly, then

wc.DownloadDataAsync(new Uri(photo.MediumUrl));

should work.

Upvotes: 0

Thomas Levesque
Thomas Levesque

Reputation: 292625

objimage = new MemoryStream(wc.DownloadData(new Uri(photo.MediumUrl)));

b) I have tried Uri myuri = new uri(string) - errors out as above.

This is the usual way to create a Uri from a string... I don't see why it wouldn't work if the string is a valid URI

Upvotes: 5

Related Questions