1

这是我遇到的问题。我有一个包含几列的 SQL Server 表。其中之一是image数据类型。图像保存为二进制数据。

在一个表单上,我有一个 dataGridView,它显示了表中的 3 列。它们都不包含图像。当我单击 dataGridView 中的记录时,我希望图像显示在同一表单的图片框中。

这是我到目前为止的代码:

public void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
        SqlConnection cn = new SqlConnection(@" Data Source=HOME-D2CADC8D4F\SQL;Initial Catalog=motociclete;Integrated Security=True");

        if (e.RowIndex >= 0) {
            DataGridViewRow row = dataGridView1.SelectedRows[0];
            textBox1.Text = row.Cells["anf"].Value.ToString();
            textBox2.Text = row.Cells["putere"].Value.ToString();
            textBox3.Text = row.Cells["caprez"].Value.ToString();
            textBox4.Text = row.Cells["greutate"].Value.ToString();
            textBox5.Text = row.Cells["stoc"].Value.ToString();
            textBox6.Text = row.Cells["pret"].Value.ToString();
            textBox7.Text = row.Cells["garantie"].Value.ToString();

            SqlCommand cmd = new SqlCommand("select poza from motociclete where codm '" + dataGridView1.SelectedRows + "'", cn);

            cn.Open();

            try
            {
                SqlDataReader dr = cmd.ExecuteReader();

                if (dr.Read())
                {
                   byte[] picarr = (byte[])dr["poza"];
                    MemoryStream ms = new MemoryStream(picarr);
                    ms.Seek(0, SeekOrigin.Begin);
                    pictureBox1.Image = Image.FromStream(ms);
               }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally {
                cn.Close();
            }
        }
    }

我认为这是一个语法错误,但我不知道在哪里或如何修复它。

4

4 回答 4

1

我建议您将图像保存在某个临时文件夹中,然后使用 ImageLocation 属性显示它。

if (dr.Read())
{
       byte[] picarr = (byte[])dr["poza"];
       string imagePath = "path to temp folder/image123.jpg";
       File.WriteAllBytes(imagePath ,picarr);
       pictureBox1.ImageLocation = imagePath;
}

这种方法的主要优点是您可以轻松实现某种缓存,这样您就不必每次都从数据库中检索最近选择的图像。如果您的应用程序很忙,这会对应用程序性能产生很大影响。

于 2013-05-20T09:25:58.180 回答
0

您的内存流中缺少字节数组。所以用像这样的字节数组填充你的内存流

byte[] picarr = (byte[])dr["poza"];
MemoryStream ms = new MemoryStream(picarr);
pictureBox1.Image = Image.FromStream(ms);
于 2013-05-19T15:32:23.117 回答
0

从您的以下代码中:

        SqlDataReader dr;
        try
        {
            dr = cmd.ExecuteReader();
            if (dr.Read())
            {
               byte[] picarr = (byte[])dr["poza"];
                MemoryStream ms = new MemoryStream();
                ms.Seek(0, SeekOrigin.Begin);
                pictureBox1.Image = Image.FromStream(ms);
           }
        }

请注意,您的 MemoryStream ms =new MemoryStream() 提供了一个没有数据的 MemoryStream。然后,您使用该空白 MemoryStream 作为图像的来源。您应该使用刚刚在上一行中读取的字节填充内存流。

于 2013-05-19T15:35:58.523 回答
0

这是一个 OLEDB 连接代码,但您可以从中获得一些想法。图像是保存为 byte[] 的位图。

int idImagen = Convert.ToInt32(listImagenes.Text.Split(':')[0]);
            ImageConverter ic = new ImageConverter();     
OleDbConnection cnx = new OleDbConnection();
            cnx.ConnectionString = "Your Conection String";
            OleDbCommand cmdx = new OleDbCommand();
            cmdx.Connection = cnx;
            cmdx.CommandText = "SELECT IC.Foto FROM ImageCriminal IC WHERE IC.IdImage=" + idImag;
            cnx.Open();
            byte[] imgRaw = (byte[])cmdx.ExecuteScalar(); //<- Here the byte[] stored in the database gets recovered and stored in imgRaw variable.
            cnx.Close();
            Image imagen = (Image)ic.ConvertFrom((byte[])imgRaw); // This has been working for me from weeks ago!! It's very important to save the files as byte[] inside the DB.
            picBox.Image = imagen;

此代码是从另一个方法调用的。动作是: - 搜索 PictureId 等于 i 的每张图片

于 2013-05-23T02:21:21.380 回答