如何在 c# 中 select 加载 windows 表单中的图片?

how can I select the pictures loaded in windows Form in c#?

我在数据库中有一些图片,我可以检索它们。为了加载这些图片,我在Windows窗体中制作了一个“Tab Control”,其中有一个“Tab page1”。当程序 运行s 时,将为每张图片创建一个包含 PictureBox(和其他一些文本框)的组框。我的图片可以加载到这些图片框中,我会有一个组框列表(gbList)。但是,我不能select这些图片期间运行。有人可以提出解决方案吗?

 private void Form2_Load(object sender, EventArgs e)
    {
        tabPage1.Controls.Clear();

        int x = 0, y = 0;
        int j = 0;
        for (int i = 0; i < output.Count - 1; i++)
        {
            PictureBox pic = new PictureBox();
            pic.SizeMode = PictureBoxSizeMode.StretchImage;
            SelectablegroupBox gb = new SelectablegroupBox();
            gb.Controls.Add(pic);

            gbList.Add(gb);

//从ProductImages中的数据库中获取图片class:(output是数据库查询的结果)

            ProductImages pI = output[i];

                imgbyte = pI.Pic;            
            using (MemoryStream ms = new MemoryStream(imgbyte))
            {
                Image img = Image.FromStream(ms);
                pic.Image = img;
            }

//添加分组框列表到标签页:

            tabPage1.Controls.Add(gbList[j]);
            gbList[j].Location = new Point(x, y);
            y += gbList[i].Height;
            j++;
              }

这是我的问题。 我希望用户能够 select 图片(然后我想保存这些 selected 项目) .但是“结果”总是空的:

var result = from s in gbList
                     where s.Focused ==true
                     select s;

            foreach (var s in result)
        { //save the selected images}
   

正如我从另一个 post 那里了解到的,我将 SelectablegroupBox" 定义为:

class SelectablegroupBox : GroupBox
{
    public SelectablegroupBox()
    {
        this.SetStyle(ControlStyles.Selectable, true);
        this.TabStop = true;
    }
    
    protected override void OnEnter(EventArgs e)
    {
        this.Focus();
        this.Invalidate();
        base.OnEnter(e);
    }
    
    protected override void OnPaint(PaintEventArgs pe)
    {
        base.OnPaint(pe);
        if (this.Focused)
        {
            var rc = this.ClientRectangle;
            rc.Inflate(-2, -2);
            ControlPaint.DrawFocusRectangle(pe.Graphics, rc);
        }
    }
}

提前致谢

您的classSelectableGroupBox不适合让用户select一张或多张图片。您的应用程序中最多可以有一个焦点控件 - 这可能是您表单上的一个按钮,用户单击该按钮可以保存 selected 图像。
一种简单的解决方案是使用 CheckBox 控件和 Appearance property set to Button. Also, you don't have to layout the images manually, let a FlowLayoutPanel 来完成这项工作。
首先,将名为 flowLayoutPanelFlowLayoutPanel 添加到您的 tabPage2 并设置以下属性:

flowLayoutPanel.AutoScroll = true;
flowLayoutPanel.Dock = System.Windows.Forms.DockStyle.Fill;

然后将Form2中相应的代码改为:

private const int imageWidth = 128;
private const int imageHeight = 128;

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    for (int i = 0; i < output.Count; i++)
    {
        CheckBox cb = new CheckBox();
        cb.Appearance = Appearance.Button;
        cb.Size = new Size(imageWidth, imageHeight);
        cb.BackgroundImageLayout = ImageLayout.Zoom;
        ProductImages pI = output[i];
        //Don't dispose the MemoryStream, the Image class will need it!
        var ms = new MemoryStream(pI.Pic);
        cb.BackgroundImage = Image.FromStream(ms);
        flowLayoutPanel.Controls.Add(cb);
    }
}

这就是您获取 selected 图片的方式:

private void SaveButton_Click(object sender, EventArgs e)
{
    var selected = flowLayoutPanel.Controls.OfType<CheckBox>().Where(x => x.Checked);
    Debug.Print("Selected images: {0}", selected.Count());
    foreach (var item in selected)
    {
        //Save the picture from item.BackgroundImage.
    }
}