如果要将其存储在数据库中,则应使用varbinary
。它在功能上等同于image
列类型,但image
已弃用,因此支持varbinary(MAX)
。通常,要使用数据库中的图像,您需要创建一个HttpHandler(或使用ASP.NET MVC的控制器/操作),该控制器处理与图像关联的标识符的url编码,例如http://www.example.com/imagehandler.ashx?id=42
。在这种情况下,处理程序将知道在数据库中找到ID为42
的图像,并使用正确的MIME类型构造对应于列内容的字节数组的响应。
示例代码:没有经过充分测试
public class ImageHandler: IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
int imageID = -1;
if (context.Request.QueryString["id"] == null || !int.TryParse(context.Request.QueryString["id"], out imageID))
{
throw new ArgumentException("Invalid or missing image id.");
}
using (var context = new ImagesDataContext())
{
var image = context.Images.SingleOrDefault(i => i.ID == imageID);
if (image != null)
{
context.Response.ContentType = image.MimeType;
context.Response.BinaryWrite(image.Content);
}
else
{
// decide how you want to handle an image that isn't found
}
}
}
public bool IsReusable
{
get { return false; }
}
}
我很抱歉,但这个问题的书面让我头痛。即使英语不是您的第一语言,如果您希望获得高质量的答案,请花时间以合理和一致的方式阐述您的问题。 – Yuck
jajaja thanks @Yuck – angel
有关varbinary和image之间区别的解释,请参阅:http://stackoverflow.com/questions/4113294/is-there-a-big-technical-difference-between-varbinarymax-and-image -data-类型/ 4113417#4113417 – kristian