URL에서 이미지를 다운로드하는 방법
URL이 링크 끝에 이미지 형식이없는 경우 C #의 URL에서 직접 이미지를 다운로드하는 방법이 있습니까? URL의 예 :
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_110e04e71d969d392b63b27ec4f4b24a
URL이 이미지 형식으로 끝날 때 이미지를 다운로드하는 방법을 알고 있습니다. 예 :
http://img1.wikia.nocookie.net/__cb20101219155130/uncyclopedia/images/7/70/Facebooklogin.png
간단히 다음과 같은 방법을 사용할 수 있습니다.
using (WebClient client = new WebClient())
{
client.DownloadFile(new Uri(url), @"c:\temp\image35.png");
// OR
client.DownloadFileAsync(new Uri(url), @"c:\temp\image35.png");
}
이러한 메서드는 DownloadString (..) 및 DownloadStringAsync (...)와 거의 동일합니다. C # 문자열이 아닌 디렉토리에 파일을 저장하며 URi에서 형식 확장자가 필요하지 않습니다.
이미지의 형식 (.png, .jpeg 등)을 모르는 경우
public void SaveImage(string filename, ImageFormat format)
{
WebClient client = new WebClient();
Stream stream = client.OpenRead(imageUrl);
Bitmap bitmap; bitmap = new Bitmap(stream);
if (bitmap != null)
{
bitmap.Save(filename, format);
}
stream.Flush();
stream.Close();
client.Dispose();
}
그것을 사용
try
{
SaveImage("--- Any Image Path ---", ImageFormat.Png)
}
catch(ExternalException)
{
// Something is wrong with Format -- Maybe required Format is not
// applicable here
}
catch(ArgumentNullException)
{
// Something wrong with Stream
}
이미지 형식을 알고 있는지 여부에 따라 다음과 같은 방법으로 수행 할 수 있습니다.
이미지 형식을 알고 이미지를 파일로 다운로드
using (WebClient webClient = new WebClient())
{
webClient.DownloadFile("http://yoururl.com/image.png", "image.png") ;
}
이미지 형식을 모르고 이미지를 파일로 다운로드
You can use Image.FromStream
to load any kind of usual bitmaps (jpg, png, bmp, gif, ... ), it will detect automaticaly 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) ;
}
}
}
Note : ArgumentException may be thrown by Image.FromStream
if the downloaded content is not a known image type.
Check this reference on MSDN to find all format available. Here are reference to WebClient
and Bitmap
.
For anyone who wants to download an image WITHOUT saving it to a file:
Image DownloadImage(string fromUrl)
{
using (System.Net.WebClient webClient = new System.Net.WebClient())
{
using (Stream stream = webClient.OpenRead(fromUrl))
{
return Image.FromStream(stream);
}
}
}
.net Framework allows PictureBox Control to Load Images from url
and Save image in Laod Complete Event
protected void LoadImage() {
pictureBox1.ImageLocation = "PROXY_URL;}
void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e) {
pictureBox1.Image.Save(destination); }
Try This It should Definitely Work 100%
Write This in Your Controller File
public class DemoController: Controller
public async Task<FileStreamResult> GetLogoImage(string logoimage)
{
string str = "" ;
var filePath = Server.MapPath("~/App_Data/" + SubfolderName);//If subfolder exist otherwise leave.
// DirectoryInfo dir = new DirectoryInfo(filePath);
string[] filePaths = Directory.GetFiles(@filePath, "*.*");
foreach (var fileTemp in filePaths)
{
str= fileTemp.ToString();
}
return File(new MemoryStream(System.IO.File.ReadAllBytes(str)), System.Web.MimeMapping.GetMimeMapping(str), Path.GetFileName(str));
}
Here is my View
<div><a href="/DemoController/GetLogoImage?Type=Logo" target="_blank">Download Logo</a></div>
참고URL : https://stackoverflow.com/questions/24797485/how-to-download-image-from-url
'programing tip' 카테고리의 다른 글
하나의 MySQL 테이블을 다른 값으로 업데이트 (0) | 2020.09.16 |
---|---|
연산자 [] [] 오버로드 (0) | 2020.09.16 |
텍스트에 줄임표 효과를 적용하는 방법 (0) | 2020.09.15 |
✔ UITableViewCell에서 선택한 행에 체크 표시 (0) | 2020.09.15 |
부모 클래스에서 자식 클래스로 캐스트 할 수 없습니다. (0) | 2020.09.15 |