ASP.NET MVC3 (C #) Image from url to database

This seems like a very simple problem, however after extensive searching, I cannot find what I am looking for. What is the best possible implementation for pulling an image from a URL and saving it to a SQL database?

I am using MVC3 in C # and Linq for SQL.

+3


source to share


3 answers


First download this image from url then save it to database



     string tnail = "";
  WebClient client = new WebClient();
    using (Image src = Image.FromFile("http://www.example.com/image.jpg"))
                        {
                            using (Bitmap dst = new Bitmap(25, 33))
                            {
                                using (Graphics g = Graphics.FromImage(dst))
                                {
                                    g.SmoothingMode = SmoothingMode.HighQuality;
                                    g.InterpolationMode = InterpolationMode.High;
                                    g.DrawImage(src, 0, 0, dst.Width, dst.Height);
                                }
                                tnail = tname;
                                tnail = Path.ChangeExtension(tnail, null);
                                tnail += "_thumbnail";
                                tnail = Path.ChangeExtension(tnail, "jpg");
                                dst.Save(Path.Combine(Server.MapPath(imagepath), tnail), ImageFormat.Jpeg);

                            }
                        }

      

+1


source


Perhaps the best way is not to store it directly in the database, but to store a link to a central file store.



However, you can have a look at Embed image via 3mb via linq-to-sql

0


source


If we assume that you have a persistent object (named ImageFile

below) that has a byte array property that represents a file, you can use [WebClient.DownloadData][1]

.

using(WebClient client = new WebClient())
{
    var img = new ImageFile();
    img.Data = client.DownloadData("http://www.example.com/image.jpg");

    // continue with saving your file.    
}

      

0


source







All Articles