Sunday, September 21, 2014

  • C# Load Image from URL ASP

    Loading an image from a URL in C# is possible without much code. Downloading images off the internet can be done directly to memory without having to save them as a file. The image in memory can be written to disk later if necessary.



     The Logic Behind   is to wrap the raw data as a Stream. The System.IO namespace in C# has a useful class called MemoryStream. The MemoryStream C# class can be loaded with raw data that will be read like any other "file" stream, except the bytes are in memory.


    byte[] imageData = DownloadData(Url); //Url is image link
    MemoryStream stream = new MemoryStream(imageData);
    Image img = Image.FromStream(stream);
    stream.Close();
     string saveImagePath = System.Web.HttpContext.Current.Server.MapPath("../media/full/")  + "image.jpg"; 
     img.Save(saveImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);


  • 0 comments:

    Post a Comment

    Copyright @ 2013 Code Snippets.