私たちは、このようにURIを持っている場合:Uriの他のウェブサイトからMVCとC#で画像バイト[]を取得する方法は?
uri = new Uri(info.ImageAddress);
と画像アドレスは、このアドレスを持っている:
http://www.pictofigo.com/assets/uploads/pictures/0/3466/thumb-vacation-pictofigo-hi-005.png
私はMVCの画像データを取得できますか?
私たちは、このようにURIを持っている場合:Uriの他のウェブサイトからMVCとC#で画像バイト[]を取得する方法は?
uri = new Uri(info.ImageAddress);
と画像アドレスは、このアドレスを持っている:
http://www.pictofigo.com/assets/uploads/pictures/0/3466/thumb-vacation-pictofigo-hi-005.png
私はMVCの画像データを取得できますか?
answer質問How to download image from url using c#にお手伝いください。
Image.FromStream
を使用すると、通常のビットマップ(jpg、png、bmp、gif、など)を読み込むことができます。自動的にファイルの種類が検出されます。非常に良い練習ではありません)。例:
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);
}
}
}
もちろん、これは大きなファイルでもよいので、非同期呼び出しです。