Reputation: 2197
if we have Uri like this :
uri = new Uri(info.ImageAddress);
and image address has this address:
http://www.pictofigo.com/assets/uploads/pictures/0/3466/thumb-vacation-pictofigo-hi-005.png
How can i get image data in mvc?
Upvotes: 0
Views: 2262
Reputation: 467
This answer to the question How to download image from url using c# should help you.
You can use Image.FromStream
to load any kind of usual bitmaps (jpg, png, bmp, gif, ... ), it will detect automatically the file type and you don't even need to check the url extension (which is not a very good practice). E.g.:
using (WebClient webClient = new WebClient())
{
byte [] data = webClient.DownloadData("https://fbcdn-sphotos-h-a.akamaihd.net/hphotos-ak-xpf1/v/t34.0-12/10555140_10201501435212873_1318258071_n.jpg?oh=97ebc03895b7acee9aebbde7d6b002bf&oe=53C9ABB0&__gda__=1405685729_110e04e71d9");
using (MemoryStream mem = new MemoryStream(data))
{
using (var yourImage = Image.FromStream(mem))
{
// If you want it as Png
yourImage.Save("path_to_your_file.png", ImageFormat.Png) ;
// If you want it as Jpeg
yourImage.Save("path_to_your_file.jpg", ImageFormat.Jpeg) ;
}
}
}
Upvotes: 2
Reputation: 3009
You would need to use something like a HttpClient call and stream the file back into a filestream which can then be used to save the file to a http response or saved directly to disk:
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(HttpMethod.Get, requestUri))
{
using (
Stream contentStream = await(await httpClient.SendAsync(request)).Content.ReadAsStreamAsync(),
stream = new FileStream("MyImage", FileMode.Create, FileAccess.Write, FileShare.None, Constants.LargeBufferSize, true))
{
await contentStream.CopyToAsync(stream);
}
}
}
This is of course an async call as it could be a large file.
Upvotes: 0