将图像存储在数据库中会产生部分图像 [英] Storing images in database results in partial image

查看:99
本文介绍了将图像存储在数据库中会产生部分图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个sql server数据库中存储图像。我有一个存储数据的图像表中的列,是类型varbinary(max)。我使用NHibernate来访问数据库。

将图像加载到代码中并将其转换为缓冲区数组工作正常。当我将图像存储在数据库中时,无论我放入30kb以上的图像大小如何,只有部分图像被保存。



我检查了存储在数据库和所有的图像有相同数量的数据存储,所以我的猜测是有限的可以在列中保存的字节[]的大小。

当我从数据库中提取数据并在屏幕上显示图像时,它只显示图像的顶部。



有什么可能是错的?



更新:
我检查了varbinary(max)列中数据的大小,所有数据项都是8000bytes。



以下是代码:

表格创建:

  IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE'Image')
CREATE TABLE [Image]

[ImageId] INT IDENTITY(1, 1)NOT NULL,
[FileName] NVARCHAR(MAX)NOT NULL,
[ImageData] VARBINARY(MAX)NOT NULL,
[Caption] NVARCHAR(MAX)NULL,
CONSTRAINT PK_ImageId PRIMARY KEY CLUSTERED(ImageId ASC)
ON [PRIMARY]

流利的映射:

  public ImageMap()
{
Id(x => x.Id,ImageId)。GeneratedBy.Identity ();
Map(x => x.Caption);
Map(x => x.FileName);
Map(x => x.Data).Column(ImageData);
HasMany(x => x.ArticleImages).KeyColumn(ImageId)。Inverse();
HasOne(x => x.Thumbnail).PropertyRef(r => r.Image).Cascade.All();

NHibernate实体类:

  using System.Collections.Generic; 


public class Image:BaseEntity
{
///< summary>
///图片名称
///< / summary>
公共虚拟字符串FileName {get;组; }

///< summary>
///图片的标题
///< / summary>
公共虚拟字符串Caption {get;组; }

///< summary>
///图片的二进制数据
///< / summary>
public virtual byte [] Data {get;组; }

///< summary>
///链接到文章
///< / summary>
public virtual IEnumerable< ArticleImage> ArticleImages {get;组; }

///< summary>
///图片缩略图
///< / summary>
public virtual ImageThumbnail Thumbnail {get;组; }
}


解决方案

p>

将imagedata映射更改为

Map(x => x.Data).Column(ImageData)。 CustomSqlType(VARBINARY(MAX))。Length(160000);

由于某些原因,通过nhibernate映射将数据截断为8000字节。添加这个固定它。


I'm trying to store images in a sql server database. I've got a column in an Image table which stores the data and is of type varbinary(max). I'm using NHibernate to access the database.

The loading of the image into the code and converting it to a buffer array works fine. When I store the image in the database, no matter what size image above 30kb that I put in, only part of the image is saved.

I checked the data stored in the database and all images have the same amount of data stored so my guess is that something is limiting the size of the byte[] that can be held in the column.

When I pull the data out of the database and display the image on the screen, it shows the top portion of the image only.

What could be wrong?

Update: I checked the size of the data in the varbinary(max) column and all data entries are 8000bytes.

Here's the code:

Table Creation:

IF NOT EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'Image')
            CREATE TABLE [Image]
            (
                [ImageId]                       INT IDENTITY(1,1)       NOT NULL,
                [FileName]                      NVARCHAR(MAX)           NOT NULL,
                [ImageData]                     VARBINARY(MAX)          NOT NULL,
                [Caption]                       NVARCHAR(MAX)           NULL,
                CONSTRAINT PK_ImageId PRIMARY KEY CLUSTERED (ImageId ASC)
                ON [PRIMARY]
            )

Fluent mapping:

public ImageMap()
    {
        Id(x=>x.Id,"ImageId").GeneratedBy.Identity();
        Map(x => x.Caption);
        Map(x => x.FileName);
        Map(x => x.Data).Column("ImageData");
        HasMany(x => x.ArticleImages).KeyColumn("ImageId").Inverse();
        HasOne(x => x.Thumbnail).PropertyRef(r=>r.Image).Cascade.All();
    }

NHibernate entity class:

using System.Collections.Generic;


public class Image : BaseEntity
{
    /// <summary>
    /// Image's name
    /// </summary>
    public virtual string FileName { get; set; }

    /// <summary>
    /// Image's Caption
    /// </summary>
    public virtual string Caption { get; set; }

    /// <summary>
    /// Binary data for the image
    /// </summary>
    public virtual byte[] Data { get; set; }

    /// <summary>
    /// Link to article
    /// </summary>
    public virtual IEnumerable<ArticleImage> ArticleImages { get; set; }

    /// <summary>
    /// The thumbnail for the image
    /// </summary>
    public virtual ImageThumbnail Thumbnail { get; set; }
}

解决方案

Fixed it.

Changed the imagedata mapping to

Map(x => x.Data).Column("ImageData").CustomSqlType("VARBINARY(MAX)").Length(160000);

For some reason, the data was being truncated to 8000 bytes via the nhibernate mapping. Adding this fixed it.

这篇关于将图像存储在数据库中会产生部分图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆