ASP.NET MVC3 (C #) Image from url to database
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 to share
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 to share
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 to share