这似乎是一个非常简单的问题,但经过广泛搜索后,我似乎无法找到我正在寻找的东西。从URL中提取图像并将其保存到SQL数据库的最佳实现是什么?
我在C#和Linq to SQL中使用MVC3。
答案 0 :(得分:1)
首先从URL下载该图像,然后将其保存在数据库中
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 :(得分:0)
也许最好的方法不是将其直接保存在数据库中,而是存储对中央文件存储的引用。
那就是说,你可以查看Inserting image over 3mb via linq-to-sql
答案 2 :(得分:0)
如果我们假设您有一个表示文件的字节数组属性的可持久实体(下面名为ImageFile
),则可以使用[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.
}