ASP.NET MVC3(C#)从URL到数据库的映像
本文关键字:URL 数据库 映像 NET MVC3 ASP | 更新日期: 2023-09-27 18:27:03
这似乎是一个非常容易的问题,但经过广泛的搜索,我似乎找不到我要找的东西。从url中提取图像并将其保存到SQL数据库的最佳实现是什么?
我在C#中使用MVC3,在SQL中使用Linq。
首先从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);
}
}
也许最好的方法不是直接将其保存在数据库中,而是存储对中央文件存储的引用。
也就是说,您可能会看到通过linq插入3mb以上的图像到sql
如果我们假设您有一个持久实体(下面命名为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.
}