Cannot Convert System.String to System.Uri
Asked Answered
S

5

23

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));  
    }
    
Suasion answered 23/9, 2009 at 12:27 Comment(3)
which version of the .NET framework are you using? Is it Compact Framework?Hodgkinson
@Dave Rook: To indent code in a list, you need to indent each line by 8 spaces. It's a Markdown quirk. See meta.stackexchange.com/questions/3792/…Awoke
@Dave Rook: You edited this post and raised it to our attention a couple of weeks ago (which was how I was able to ping you). A bit of a delay there but I thought I'd let you know regardless.Awoke
E
44

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".

Externalize answered 23/9, 2009 at 12:40 Comment(1)
No there are no typos in my code just in the typing here. Also the URl is dynamically generated at runtime.Suasion
A
8

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.

Angola answered 27/4, 2010 at 10:26 Comment(0)
B
5
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

Boric answered 23/9, 2009 at 12:29 Comment(2)
Thanks. Tried that too ( sorry did not mention above) - no dice. Compile error.Suasion
Thie error could be due to the typo in things I have tried (b). We just can't tell.Vaticinal
A
5
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

Adlay answered 23/9, 2009 at 12:30 Comment(2)
Sorry Does'nt work; Now the error is as follows: Cannot resolve constructor: Memory Stream(void) candidates are: MemoryStream (byte[]) (in class memory stream) memoryStream (int) (in class memory stream)Suasion
public void DownloadDataAsync(System.Uri address) Member of System.Net.WebClient Summary: Downloads the specified resource as a System.Byte array. This method does not block the calling thread. Parameters: address: A System.Uri containing the URI to download.Suasion
H
0

If I understand your code correctly, then

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

should work.

Hodgkinson answered 23/9, 2009 at 12:31 Comment(1)
Yes, I want to use the Async method but I cannot because of the string to Uri thingy. The method above works fine.Suasion

© 2022 - 2024 — McMap. All rights reserved.