![](/img/trans.png)
[英]in c# website how we can connect two users so that they can pass textbox values to each other?
[英]How we can get image byte[] from other website by Uri in MVC and C#?
如果我們有這樣的Uri:
uri = new Uri(info.ImageAddress);
並且圖片地址具有以下地址:
http://www.pictofigo.com/assets/uploads/pictures/0/3466/thumb-vacation-pictofigo-hi-005.png
如何在MVC中獲取圖像數據?
如何使用c#從url下載圖像的問題的此答案應該對您有所幫助。
您可以使用Image.FromStream
加載任何常用的位圖(jpg,png,bmp,gif,...),它會自動檢測文件類型,您甚至不需要檢查url擴展名(這不是很好的做法)。 例如:
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) ;
}
}
}
您將需要使用HttpClient調用之類的東西,並將文件流式傳輸回文件流,然后可以使用該文件流將文件保存到http響應或直接保存到磁盤:
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);
}
}
}
這當然是一個異步調用,因為它可能是一個大文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.