开发者

How do i buffer an image from external link

开发者 https://www.devze.com 2023-02-04 00:05 出处:网络
I have a external link with an image which i want to stream, but i get this error when i try. 开发者_如何学Cerror

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

开发者_如何学Cerror "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


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();


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];       
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号