【问题标题】:Show the picture from database to each usercontrol's picturebox?将数据库中的图片显示到每个用户控件的图片框?
【发布时间】:2013-06-06 02:34:49
【问题描述】:

用户控制代码:

    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);
                }

            }
        }  

【问题讨论】:

    标签: c# winforms user-controls picturebox dynamic-controls


    【解决方案1】:

    要一次访问多个控件(在您的情况下为图片框),请使用 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
        }
    }
    

    【讨论】:

      【解决方案2】:

      为您的 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); 
          }
      
      }
      

      【讨论】:

      • 没有处理空值。但仍然感谢您专门为有线控制提供的。 :)
      • 预先检查空值很容易。如果没有关联的图像,您可以创建一个仅接受 LastName 的不同重载......
      【解决方案3】:

      用户控制代码:

          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);
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-08
        • 2017-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多