为什么我的图片(来自数据库)不能在我的图片框中正确显示? [英] Why is my image (from database) not displaying properly in my pictureBox?

查看:60
本文介绍了为什么我的图片(来自数据库)不能在我的图片框中正确显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样保存我的图像:

//This is in my ImageConverter class:
public static byte[] ConvertImageToByteArray(Image userImage) //Get bytes of the image
{
    using (MemoryStream ms = new MemoryStream())
    using (Bitmap tempImage = new Bitmap(userImage))
    {        
        tempImage.Save(ms, userImage.RawFormat);
        return ms.ToArray();
    }
}       

//this is in my save button:
sqlCmd.Parameters.Add("@user_image", SqlDbType.VarBinary, 8000).Value = 
    ImageConverter.ConvertImageToByteArray(pictureBox1.Image);

我通过单击 datagridview 来检索我的图像,如下所示:

I retrieve my image by clicking on the datagridview like this:

private void dgvEmpDetails_CellClick(object sender, DataGridViewCellEventArgs e)
{
    try
    {
        if (e.RowIndex != -1)
        {
            //Display user image
            using (SqlConnection con = new SqlConnection(connectionStringConfig))
            using (SqlCommand sqlCmd = new SqlCommand(
                "SELECT user_image FROM dbo.Employee_Image 
                 WHERE employee_id=@employee_id", con))
            {
                con.Open();
                sqlCmd.Parameters.Add("@employee_id", 
                    SqlDbType.NVarChar).Value = EmployeeId;

                using (SqlDataReader reader = sqlCmd.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        reader.Read();
                        pictureBox1.Image = ImageConverter.
                            ConvertByteArrayToImage((byte[])(reader.GetValue(0)));
                    }
                    else
                    {
                        pictureBox1.Image = null;
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show($"Something is wrong with the selected record! 
            \nError: { ex.Message  }");
    }
}       

//This is in my ImageConverter class:
public static Image ConvertByteArrayToImage(byte[] buffer) //Get image from database
{
    using (MemoryStream ms = new MemoryStream(buffer))
    {
        return Image.FromStream(ms);
    }
}      

注意:我不会在数据网格视图中显示图像的二进制数据.

NOTE: I don't display my image's binary data in my datagridview.

保存和更新图像(带有用户记录)工作正常.

Saving and updating the image (with the users records) works fine.

将图像保存到数据库后,无法正常显示.但是当我使用 OpenFileDialog 加载它时,图像显示得很好.

After saving an image to the database, it does not display properly. But when I load it using OpenFileDialog the image displays just fine.

使用 OpenFileDialog 加载图像:

Loading the image using OpenFileDialog:

当我单击 datagridview 行查看用户记录时,图片框如下所示:

When I click a datagridview row to view a user record this is what the pictureBox looks like:

为什么会出现这种分裂?我还没有看到任何类似的问题/解决方案.其中大部分是关于将图像从数据库加载到图片框".但我已经这样做了.

Why is this split in some sort? I have not seen any similar problem/solution about this. Most of them is about "Loading image from the database to pictureBox". But I have already done that.

推荐答案

这是我从数据库中获取图像的方法

This is my approach for getting images from database

   // This method use to update the form.
    private void loadFormWithID(int ID)
    {
        dbServer conn = new dbServer(sysController.getConn);
        DataTable tbl = conn.getQueryList("SELECT * FROM Products WHERE ID = " + ID);
        DataRow row = tbl.Rows[0];      
        // This is how i update the Picture Box
        pictureBoxItem.Image = row["Image"] == DBNull.Value ? pictureBoxItem.InitialImage : ImageController.bytesToImage((byte[])row["Image"]);  
     }

这是我与数据库通信的 dbserver 类.

This is my dbserver class which communicates with database.

   public class dbServer
   {
        public string _connectionLink;

        public dbServer(string connectionString)
        {
            _connectionLink = connectionString; 
        }

        public DataTable getQueryList(string sqlQuery)
        {
             DataTable tbl = new DataTable();

           using (SqlConnection conn = new SqlConnection(_connectionLink))
           {
               using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
                {
                   conn.Open();
                   SqlDataReader reader = cmd.ExecuteReader();
                   tbl.Load(reader);
                }
            }
            return tbl;
        }
   }

我希望这能解决问题.

这是我用于我的数据库图像检索器的.

This is what i used for my database image retriever.

class ImageController
{
    public static byte[] ImageToBytes(PictureBox pb)
    {
        MemoryStream ms = new MemoryStream();
        pb.Image.Save(ms, pb.Image.RawFormat);
        return ms.GetBuffer();
    }

    public static byte[] ImageToBytes(Image pb)
    {
        MemoryStream ms = new MemoryStream();
        pb.Save(ms, pb.RawFormat);
        Console.WriteLine(ms.Length);
        return ms.GetBuffer();
    } 

    public static Image bytesToImage(byte[] imageRaw)
    {
        MemoryStream ms = new MemoryStream(imageRaw);
        return Image.FromStream(ms);
    }
}

这篇关于为什么我的图片(来自数据库)不能在我的图片框中正确显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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