2
votes

I have a external link with an image which i want to stream, but i get this error when i try.

error "URI formats are not supported."

I tried to stream: Stream fileStream = new FileStream("http://www.lokeshdhakar.com/projects/lightbox2/images/image-2.jpg", FileMode.Open); byte[] fileContent = new byte[fileStream.Length];

can anyone put some light on this.

Thanks

2

2 Answers

4
votes

The FileStream contructor you are using must be provided with a path on your local harddrive and not with an external URL.

You are probably looking for this:

string url = "http://www.lokeshdhakar.com/projects/lightbox2/images/image-2.jpg";
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream stream = httpWebReponse.GetResponseStream();

Probably also for this:

Image pic = Image.FromStream(stream);
MemoryStream ms = new MemoryStream();
pic.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Byte[] arr = ms.ToArray();
0
votes

FileStream doesn't support the opening files over the internet.

Try this:

var webClient = new WebClient();
using(var fileStream = webClient.OpenRead("http://www.lokeshdhakar.com/projects/lightbox2/images/image-2.jpg")) 
{
    byte[] fileContent = new byte[fileStream.Length];       
}