【问题标题】:how can I select the pictures loaded in windows Form in c#?如何在c#中选择windows窗体中加载的图片?
【发布时间】:2021-09-19 20:45:42
【问题描述】:

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

 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类中从数据库中获取图片:(输出是查询数据库的结果)

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

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

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

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

我从另一篇文章中了解到,我将 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);
        }
    }
}

提前致谢

【问题讨论】:

  • 您的目标是什么:Winforms、WPF、ASP..?您应该始终正确标记您的问题,以便人们可以在问题页面上看到它! - 你确定它们显示的是 GroupBox 吗? Groupboxes 是容器,通常不接受焦点也不显示图像..
  • 我正在使用 windows 窗体。我正在寻找一种能够选择图像的方法。
  • 你写的,但是图片在哪里加载?在图片框中?面板??标签???其他控制? - 另外:“此代码不起作用” 总是一个糟糕/无用的描述!怎么了?编译错误?结果错误?没有结果? - 另外:只有 一个 控件(如果有)可以拥有焦点! FocusSelected 有很大区别!!
  • 您显示的代码根本不足以看到您的问题。尝试添加更多代码,您要从哪里选择图像以及要在哪里显示它们?
  • 我没有看到任何可以“选择”组框或图片框的代码。 Norte Focus() 仅将键盘焦点设置为控件,从而从以前可能拥有它的任何控件中窃取它。 - 你为什么不给你的班级一个Selected 属性? - 还要让用户以实用的方式进行选择,例如通过点击而不是仅仅输入!!

标签: c# winforms


【解决方案1】:

您的班级SelectableGroupBox 不适合让用户选择一张或多张图片。您的应用中最多可以有一个焦点控件 - 这可能是表单上的一个按钮,用户单击该按钮以保存所选图像。
一种简单的解决方案是使用CheckBox 控件并将Appearance 属性设置为Button。此外,您不必手动布置图像,让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);
    }
}

这是您获取所选图片的方式:

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.
    }
}

【讨论】:

  • 非常感谢@Steeeve。这是一个很大的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多