将图像存储在数据库中会导致部分图像

发布于 2024-11-02 16:34:45 字数 2191 浏览 0 评论 0原文

我正在尝试将图像存储在 SQL Server 数据库中。我在图像表中有一个列,它存储数据并且类型为 varbinary(max)。我正在使用 NHibernate 来访问数据库。

将图像加载到代码中并将其转换为缓冲区数组工作正常。当我将图像存储到数据库中时,无论我放入30kb以上的什么尺寸的图像,都只会保存部分图像。

我检查了数据库中存储的数据,所有图像都存储了相同数量的数据,所以我的猜测是某些东西限制了列中可以保存的 byte[] 的大小。

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

可能出什么问题了?

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

代码如下:

表创建:

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>
    /// 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; }
}

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; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甲如呢乙后呢 2024-11-09 16:34:45

修好了。

将 imagedata 映射更改为

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

由于某种原因,数据通过 nhibernate 映射被截断为 8000 字节。添加这个修复了它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文