Gibt es eine Möglichkeit, ein Bild direkt von einer URL in c # herunterzuladen, wenn die URL kein Bildformat am Ende des Links hat? Beispiel für eine 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
Ich kann das Bild herunterladen, wenn die URL mit einem Bildformat endet. Z.B:
http://img1.wikia.nocookie.net/__cb20101219155130/uncyclopedia/images/7/70/Facebooklogin.png
Einfach Sie können die folgenden Methoden verwenden.
using (WebClient client = new WebClient())
{
client.DownloadFile(new Uri(url), @"c:\temp\image35.png");
//OR
client.DownloadFileAsync(new Uri(url), @"c:\temp\image35.png");
}
Diese Methoden sind fast identisch mit DownloadString (..) und DownloadStringAsync (...). Sie speichern die Datei in Directory und nicht in C # -Zeichen. In URi ist keine Formaterweiterung erforderlich
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
}
Je nachdem, ob Sie das Bildformat kennen oder nicht, können Sie dies tun:
using (WebClient webClient = new WebClient())
{
webClient.DownloadFile("http://yoururl.com/image.png", "image.png") ;
}
Sie können Image.FromStream
verwenden, um alle üblichen Bitmaps (jpg, png, bmp, gif, ...) zu laden. Der Dateityp wird automatisch erkannt, und Sie müssen nicht einmal die URL-Erweiterung (welche ist keine sehr gute Praxis). Z.B:
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) ;
}
}
}
Hinweis: ArgumentException kann von Image.FromStream
ausgelöst werden, wenn der heruntergeladene Inhalt kein bekannter Bildtyp ist.
Überprüfen Sie diese Referenz auf MSDN , um alle verfügbaren Formate zu finden. Hier sind Verweise auf WebClient
und Bitmap
.
.net Framework ermöglicht PictureBox Control das Laden von Bildern von einer URL
und Bild in Load Complete Event speichern
protected void LoadImage() {
pictureBox1.ImageLocation = "PROXY_URL;}
void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e) {
pictureBox1.Image.Save(destination); }
Für alle, die ein Bild herunterladen möchten, OHNE es in einer Datei zu speichern:
Image DownloadImage(string fromUrl)
{
using (System.Net.WebClient webClient = new System.Net.WebClient())
{
using (Stream stream = webClient.OpenRead(fromUrl))
{
return Image.FromStream(stream);
}
}
}
Probieren Sie es aus, es funktioniert auf jeden Fall 100%
Schreiben Sie dies in Ihre Controller-Datei
public class TenantCustomizationController : RecruitControllerBase
//enter code here
[AllowAnonymous]
[HttpGet]
public async Task<FileStreamResult> GetLogoImage(string logoimage)
{
string str = "" ;
var filePath = Server.MapPath("~/App_Data/" + AbpSession.TenantId.Value);
// 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));
}
//09/07/2018
Here Is my View
<div><a href="/TenantCustomization/GetLogoImage?Type=Logo" target="_blank">@L("DownloadBrowseLogo")</a></div>