【问题标题】:Size of Image while storing it in database将图像存储在数据库中时的大小
【发布时间】:2016-06-08 03:55:45
【问题描述】:

我遇到了一个关于图像大小的奇怪问题。

我制作了一个简单的应用程序,可以在数据库中存储和检索图像。当我从文件中读取图像时,它的大小以 kB(千字节)为单位,字节数组的长度也是如此。

有两个图片框。 pb1 用于存储,pb2 用于加载。

我的 store() 和 load() 方法如下:

注意:openConnState() 和 CloseConnState() 是关闭和打开连接的方法。并且 byte[] img_byte 和 imgfilelength = 0 是在类中公开定义的。

商店:

private void StoreImage(string ChosenFile)
{
    try
    {
        //MemoryStream ms = new MemoryStream();
        //pb1.Image.Save(ms, ImageFormat.Jpeg);
        //img_byte = new byte[ms.Length];
        //ms.Position = 0;
        //ms.Read(img_byte, 0, img_byte.Length);

        FileInfo fileImage = new FileInfo(ChosenFile);
        imgfilelength = fileImage.Length;
        FileStream fs = new FileStream(ChosenFile, FileMode.Open, FileAccess.Read, FileShare.Read);
        img_byte = new Byte[Convert.ToInt32(imgfilelength)];
        int count, sum = 0;

        while ((count = fs.Read(img_byte, 0, Convert.ToInt32(imgfilelength))) > 0)
        {
            sum += count;
        }

        //int byteread = fs.Read(img_byte, 0, Convert.ToInt32(imgfilelength));
        fs.Close();
    }
    catch (Exception e)
    {
        throw e;
    }
}

public void storetoDB()
{
    OpenConnState(conn);
    string str = "use db2 \n insert into TableImg(Image) \n values('" + img_byte + "')";
    SqlCommand cmd = new SqlCommand(str, conn);

    try
    {
        cmd.ExecuteNonQuery();
    }
    catch (Exception e)
    {
        throw e;
    }
    finally
    {
        CloseConnState(conn);
    }
}

加载:

public void Loadimg()
{
    try
    {
        pb2.Image = null;
        byte[] getbyte = LoadImagefromDB(3);

        using (MemoryStream ms = new MemoryStream(getbyte))
        {
            pb2.Image = Image.FromStream(ms);
        }
        pb2.Refresh();

    }
    catch (Exception e)
    {
        throw e;
    }
}

public byte[] LoadImagefromDB(long pid)
{
    byte[] img = null;
    OpenConnState(conn);
    string str = "use db2 \n select Image from TableImg where P_Id = " + pid;
    SqlCommand cmd = new SqlCommand(str, conn);

    try
    {
        img = (byte[])cmd.ExecuteScalar();

        return img;
    }
    catch (System.Exception e)
    {
        throw e;
    }
    finally
    {
        CloseConnState(conn);
    }
}

我使用上面给出的 storeDB() 方法将图像存储到数据库中,但是当我使用上面给出的 load() 方法检索图像时,我收到一条错误消息,提示 参数无效。我发现问题可能与字节数组的长度有关,因为当我将数据库的'image'数据类型值检索到字节数组中时,字节数组的长度将始终为13。

我什至运行了下面的查询来获取它在数据库中的大小,它仍然是相同的,即 13 个字节。

从 P_Id = 1 的 TableImg 中选择 len(Convert(varbinary(max), Image))

谁能告诉我,为什么?

【问题讨论】:

  • 这是旧的但是...您基本上应该将图像转换为 base64 字符串然后存储字符串...dailycoding.com/posts/…

标签: c# sql-server database image winforms


【解决方案1】:

我将数据库的“图像”数据类型值检索到一个字节数组中, 字节数组的长度总是13。

你正在尝试这样做:

use db2 \n insert into TableImg(Image) \n values('System.Byte[]')

显然,字符串System.Byte[] 的长度总是 13。

您必须在插入之前将该二进制数据转换为其他类型。

根据this post,如果你的图片很小,可以存储为VARBINARY类型。如果它很大,您应该将其作为文件存储在驱动器中。

编辑

你可以这样使用:

using (SqlCommand cmd = new SqlCommand("use db2 \n insert into TableImg(Image) \n values(@binaryValue)", conn))
{
    cmd.Parameters.Add("@binaryValue", SqlDbType.VarBinary, img_byte.Length).Value = img_byte;
    cmd.ExecuteNonQuery();
}

【讨论】:

  • 转换给我的字节数组的长度是原来的 3 倍。该死的..认真3次!就像它是 5kB 一样,它会在加载时返回 15kB。这意味着在数据库中它占用了 15kB。
  • 如果转换正确,那么我的列在数据库中的数据类型应该是什么!!?我把它当成“形象”了。
  • 根据this post,如果你的图片很小,可以存储为VARBINARY类型。
  • @SagarMaru 仍然,如果将图像插入 SQL DB,则不能使用纯二进制。请参阅我编辑的答案以获取建议
  • 哇..谢谢..我明白了!!!您更新了对 img_byte.Length 的回答。我将其标记为答案!
猜你喜欢
  • 2021-11-26
  • 2019-12-23
  • 2012-10-17
  • 2021-12-08
  • 2014-12-14
  • 2012-01-14
相关资源
最近更新 更多