显示来自数据库的图片到每个usercontrol's的图片框

本文关键字:usercontrol 数据库 显示 | 更新日期: 2023-09-27 18:16:54

Usercontrol Code:

    private string lastName;
    public string LastName
    {
        get { return lastName; }
        set
        {
            lastName = value;
            textBox1.Text = value;
        }
    }

形式代码:

        using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
        {
            myDatabaseConnection.Open();
            using (SqlCommand SqlCommand = new SqlCommand("Select LastName, Image from Employee", myDatabaseConnection))
            {

                int i = 0;
                SqlDataReader DR1 = SqlCommand.ExecuteReader();
                while (DR1.Read())
                {
                    i++;
                    UserControl2 usercontrol = new UserControl2();
                    usercontrol.Tag = i;
                    usercontrol.LastName = (string)DR1["LastName"];
                    flowLayoutPanel1.Controls.Add(usercontrol);
                }

            }
        }
使用上面的代码,我可以在每个用户控件的文本框中显示来自数据库的每个LastName。如何显示从数据库到每个用户控件的图片框的图片?

这是我如何从数据库显示单个图像:

        using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
        {
            myDatabaseConnection.Open();
            using (SqlCommand SqlCommand = new SqlCommand("Select LastName, Image from Employee where ID = @a", myDatabaseConnection))
            {
                SqlCommand.Parameters.AddWithValue("@a", textBox1.Text);
                DataSet DS = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(SqlCommand);
                da.Fill(DS, "Images");
                var imagesTable = DS.Tables["Images"];
                var imagesRows = imagesTable.Rows;
                var count = imagesRows.Count;
                if (count <= 0)
                    return;
                var imageColumnValue =
                    imagesRows[count - 1]["Image"];
                if (imageColumnValue == DBNull.Value)
                    return;
                var data = (Byte[])imageColumnValue;
                using (var stream = new MemoryStream(data))
                {
                    pictureBox1.Image = Image.FromStream(stream);
                }
            }
        }  

显示来自数据库的图片到每个usercontrol's的图片框

一次访问多个控件(在您的情况下是PictureBoxes)使用foreach循环。

foreach (Control control in this.Controls)
{
    if (control is PictureBox)
    {
        PictureBox pic = (PictureBox)control;
        pic.Image = Image.FromStream(stream); //something similar, this will only load the same image to every PictureBox
    }
}

为您的UserControl构造函数创建一个重载,以接收姓氏和图像字节。然后你可以在循环中这样做:

            while (DR1.Read())
            {
                i++;
                UserControl2 usercontrol = new UserControl2((string)DR1["LastName"], (Byte[])DR1["Image"]);
                usercontrol.Tag = i;
                flowLayoutPanel1.Controls.Add(usercontrol);
            }

这是更新的UserControl(与InvokeOnClick()代码从你的其他问题)。注意,在新的构造函数中,我们是如何从字节创建图像并将其分配给PictureBox的:

public partial class UserControl2 : UserControl
{
    private string lastName;
    public string LastName
    {
        get { return lastName; }
        set
        {
            lastName = value;
            textBox1.Text = value;
        }
    }
    public UserControl2(string LastName, byte[] data)
    {
        InitializeComponent();
        WireAllControls(this);
        this.LastName = LastName;
        try
        {
            using (var stream = new System.IO.MemoryStream(data))
            {
                pictureBox1.Image = Image.FromStream(stream);
            }
        }
        catch (Exception)
        {
            MessageBox.Show("Error Loading Image for " + LastName);
        }
    }
    public UserControl2()
    {
        InitializeComponent();
        WireAllControls(this);
    }
    private void WireAllControls(Control cont)
    {
        foreach (Control ctl in cont.Controls)
        {
            ctl.Click += ctl_Click;
            if (ctl.HasChildren)
            {
                WireAllControls(ctl);
            }
        }
    }
    private void ctl_Click(object sender, EventArgs e)
    {
        this.InvokeOnClick(this, EventArgs.Empty); 
    }
}

Usercontrol Code:

    public void showpictures()
    {
        {
            using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
            {
                myDatabaseConnection.Open();
                using (SqlCommand SqlCommand = new SqlCommand("Select LastName, Image from Employee where LastName = @a", myDatabaseConnection))
                {
                    SqlCommand.Parameters.AddWithValue("@a", textBox1.Text);
                    DataSet DS = new DataSet();
                    SqlDataAdapter da = new SqlDataAdapter(SqlCommand);
                    da.Fill(DS, "Images");
                    var imagesTable = DS.Tables["Images"];
                    var imagesRows = imagesTable.Rows;
                    var count = imagesRows.Count;
                    if (count <= 0)
                        return;
                    var imageColumnValue =
                        imagesRows[count - 1]["Image"];
                    if (imageColumnValue == DBNull.Value)
                        return;
                    var data = (Byte[])imageColumnValue;
                    using (var stream = new MemoryStream(data))
                    {
                        pictureBox1.Image = Image.FromStream(stream);
                    }

                }
            }
        }
    }

形式
            while (DR1.Read())
            {
                i++;
                UserControl2 usercontrol = new UserControl2();
                usercontrol.Tag = i;
                usercontrol.LastName = (string)DR1["LastName"];
                usercontrol.showpictures();
                flowLayoutPanel1.Controls.Add(usercontrol);
            }