简体   繁体   English

无法将byte []转换为图像

[英]unable to convert byte[] to image

I am trying to retrieve a picture stored in MS SQL Server database. 我正在尝试检索存储在MS SQL Server数据库中的图片。 The type of the column is image. 列的类型是图像。 My code is: 我的代码是:

try
{
    SqlConnection con = new SqlConnection(Properties.Settings.Default.ConnectionString);
    SqlCommand cmd = new SqlCommand(string.Empty, con);
    cmd.CommandText = "select Picture from Person";
    con.Open();
    SqlDataReader dataReader = cmd.ExecuteReader();
    dataReader.Read();

    byte[] image = new byte[10000];
    long len = dataReader.GetBytes(0, 0, image, 0, 10000);

    using (MemoryStream stream = new MemoryStream(image))
    {
        stream.Seek(0, SeekOrigin.Begin);
        pictureBox1.Image = Image.FromStream(stream);
    }
    con.Close();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

I am continuously getting ArgumentException as Parameter is not valid when I set the pictureBox1.Image property. 设置pictureBox1.Image属性时,我不断收到ArgumentException因为参数无效 I tried all the available solutions on the Internet but all in vain. 我尝试了Internet上所有可用的解决方案,但徒劳无功。

You are always using a 10000 byte array even if the image is smaller (or larger). 即使图像较小(或较大),也始终使用10000字节数组。 Don't manually create the byte[] , the DataReader can provide the entire byte array if you ask for it. 不要手动创建byte[] ,如果需要, DataReader可以提供整个字节数组。

byte[] image = reader.GetFieldValue<byte[]>(0);

If you are not using .NET 4.5 you can just ask for the field and cast it manually. 如果您不使用.NET 4.5,则可以要求字段并手动进行转换。

byte[] image = (byte[])reader.GetValue(0);

However the fact that you are only using the first column from the first row you don't need a DataReader at all, just use ExecuteScalar() instead. 但是,您只使用第一行中的第一列这一事实根本不需要DataReader ,而只需使用ExecuteScalar() (I also am cleaning up your code to use proper using statements and switched your ex.Message to ex.ToString() to provide more info in the error dialog). (我也正在清理您的代码以使用正确的using语句,并将ex.Message切换为ex.ToString()以便在错误对话框中提供更多信息)。

try
{
    using(SqlConnection con = new SqlConnection(Properties.Settings.Default.ConnectionString))
    using(SqlCommand cmd = new SqlCommand(string.Empty, con))
    {
        cmd.CommandText = "select Picture from Person";
        con.Open();

        byte[] image = (byte[])cmd.ExecuteScalar();

        using (MemoryStream stream = new MemoryStream(image))
        {
            pictureBox1.Image = Image.FromStream(stream);
        }
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

Try this : 尝试这个 :

SqlConnection con = new SqlConnection(Properties.Settings.Default.ConnectionString);
SqlCommand cmd = new SqlCommand(string.Empty, con);
cmd.CommandText = "select Picture from Person";
con.Open();
SqlDataReader dataReader = cmd.ExecuteReader();
dataReader.Read();
byte[] image = new byte[10000];
long len = dataReader.GetBytes(0, 0, image, 0, 10000);
using (MemoryStream mStream = new MemoryStream(image))
{
    pictureBox1.Image = Image.FromStream(mStream);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM