我需要保存一张jpg图片到数据库 在该属性的数据库类型中是 nvarchar(max) 所以我需要像这样序列化和反序列化这个属性:
private byte[] HexStringToObject(string value)
{
SoapHexBinary shb = SoapHexBinary.Parse(value);
return shb.Value;
}
private string ObjectToHexString(object value)
{
if (value == null)
return "";
SoapHexBinary shb = new SoapHexBinary((byte[])value);
return shb.ToString();
}
但是有一个问题,我不知道这个问题与序列化或反序列化方法有关 就在我想通过以下方式查看此图像时:
public FileResult Initialpicture(int? propertyId)
{
if (propertyId == null)
return null;
IProperty image = Repository<IProperty>.Get(propertyId);
if (image.Value == null)
return null;
return File((byte[])image.Value, "image/jpg");//Get of Value use deserialize method to get byte[]
在浏览器中我只能看到图像的一小部分,以及错误“图像损坏或截断:”
问题是什么?
最佳答案
可能您的数据库或 ORM(如果有的话)会将字节数组截断为 12000 字节。
关于c# - 使用 byte[] 和 varbinary(max) 时图像在 NHibernate 中损坏或截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8543103/