¿Hay alguna manera de descargar una imagen directamente desde una url en c # si la url no tiene un formato de imagen al final del enlace? Ejemplo de url:
http://sofes.miximages.com/c%23/10555140_10201501435212873_1318258071_n.jpg?oh=97ebc03895b7acee9aebbde7d6b002bf&oe=53C9ABB0&__gda__=1405685729_110e04e71d969d392b63b27ec4f4b24a
Sé cómo descargar la imagen cuando la url termina con un formato de imagen. P.ej:
http://sofes.miximages.com/c%23/Facebooklogin.png
Simplemente puede usar los siguientes métodos.
using (WebClient client = new WebClient()) { client.DownloadFile(new Uri(url), @"c:\temp\image35.png"); //OR client.DownloadFileAsync(new Uri(url), @"c:\temp\image35.png"); }
Estos métodos son casi los mismos que DownloadString (..) y DownloadStringAsync (…). Almacenan el archivo en el directorio en lugar de en la cadena C # y no es necesaria la extensión de formato en URi
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 }
Según si conoce o no el formato de la imagen, aquí hay formas en que puede hacerlo:
using (WebClient webClient = new WebClient()) { webClient.DownloadFile("http://yoururl.com/image.png", "image.png") ; }
Puede usar Image.FromStream
para cargar cualquier tipo de bitmap habitual (jpg, png, bmp, gif, …), detectará automáticamente el tipo de archivo y ni siquiera necesitará verificar la extensión de la url (que no es una muy buena práctica). P.ej:
using (WebClient webClient = new WebClient()) { byte [] data = webClient.DownloadData("http://sofes.miximages.com/c%23/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) ; } } }
Nota: Image.FromStream
puede lanzar ArgumentException si el contenido descargado no es un tipo de imagen conocida.
Consulte esta referencia en MSDN para encontrar todo el formato disponible. Aquí hay una referencia a WebClient
y Bitmap
.
.net Framework permite que PictureBox Control cargue imágenes desde la URL
y Guardar imagen en Laod Complete Event
protected void LoadImage() { pictureBox1.ImageLocation = "PROXY_URL;} void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e) { pictureBox1.Image.Save(destination); }
Prueba esto, definitivamente funcionará al 100%
Escribe esto en tu archivo de controlador
public class TenantCustomizationController : RecruitControllerBase //enter code here [AllowAnonymous] [HttpGet] public async Task 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