3

This seems like a really easy problem however after extensive searching I can't seem to find what I'm looking for. What is the best possible implementation for pulling an image from a url and saving it to a SQL database?

I'm using MVC3 in C# and Linq to SQL.

3 Answers 3

1

First download that image from URL then save it in 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);

                            }
                        }
Sign up to request clarification or add additional context in comments.

1 Comment

Image.FromFile() does not accept uri as an argument as it would throw ArgumentException: msdn.microsoft.com/en-us/library/stf701f5(v=vs.110).aspx
0

Maybe the best way is not to save it directly in the database, but to store a reference to a central file storage.

That said, you might have a look at Inserting image over 3mb via linq-to-sql

Comments

0

If we assume you have a persistable entity (named ImageFile below) that has a byte array property representing the 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.    
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.