单击时如何从动态图片框中获取Image [] [英] How to get Image[] from dynamic picture box when clicked

查看:69
本文介绍了单击时如何从动态图片框中获取Image []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual C#中创建了一个Panel。在page_Load事件的运行时,我动态地将PictureBox添加到Panel。现在Picture已成功添加到该面板,如下所示

I have created a Panel in Visual C#. At Runtime on page_Load event i added PictureBox to Panel dynamically. Now Picture added successfully to that Panel as follow

private void Form1_Load(object sender, EventArgs e)
        {
            DataTable dt =controller.Category_SelectList();
            List<picturebox> pictureBoxList = new List<picturebox>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                PictureBox picture = new PictureBox
                {
                    Name = "pictureBox" + i,
                    
                    Size = new Size(100, 100),
                    Location = new Point(i * 105, 1),
                    BorderStyle = BorderStyle.FixedSingle,
                    SizeMode = PictureBoxSizeMode.Zoom
                   
                };
                image = (byte[])dr["c_CatPhoto"];
                picture.Image = ByteArrToImage(image);
                picture.Name=dr["c_CatName"].ToString();
                Label lb = new Label();
                lb.Name = "label";
                lb.Text = dr["c_CatName"].ToString();
                lb.Size = new System.Drawing.Size(100, 40);
                lb.Location = new Point(i * 105, 105);
                
                //picture.ImageLocation = dr["c_CatPhoto"].ToString();
                pictureBoxList.Add(picture);
                pnlDisplayImage.Controls.Add(lb);

            }

            foreach (PictureBox p in pictureBoxList)
            {
                pnlDisplayImage.Controls.Add(p);
            }
        }
      
        public Image ByteArrToImage(byte[] brr)
        {
            MemoryStream ms = new MemoryStream(brr);
            Image img = Image.FromStream(ms);
            return img;
        }



当我点击动态图片框时,我想将相关数据显示到datagridview检索表格数据库。

但是我有问题怎么样单击动态PictureBox生成事件(如PictureBox_Click)以及如何获取图像值。请给我代码和说明,如何在我的Visual C#桌面应用程序中为PictureBox编写事件代码。


I want to show related data into datagridview retrieve form database when i clicked dynamic picturebox.
But I have problem how to Generate event (Such as PictureBox_Click ) on Click that Dynamic PictureBox and how can i get image value. Please Give me Code and Instruction, how to write event code for PictureBox in my Visual C# Desktop Application.

推荐答案

//create click event as below 
picture.Click += this.PictureClick;
//after add the picture to pictrebox 
pictureBoxList.Add(picture);



你可以创建如下事件


you can create event as below

private void PictureClick(object sender, EventArgs e) {
    PictureBox oPictureBox = (PictureBox)sender;
    // if name is unique value for the data, you can get the name 
    // and find that name in your datatable
    string c_CatName = oPictureBox.Name;

}





如果您有其他唯一的密钥列,请尝试使用标签 [ ^ ]属性PictureBox并将其设置为唯一键值,然后您可以在事件处理程序中获取它并根据该唯一键从数据表中获取详细信息。



if you have some other unique key column, try using Tag[^] property of PictureBox and set it as the unique key value, then you can get it inside event handler and take the details from datatable based on that unique key.


这篇关于单击时如何从动态图片框中获取Image []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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