繁体   English   中英

C#如何在TabPage中显示图像(资源)数组

[英]C# How to Display Image(resource) array in TabPage

目标:当应用程序启动时,我想在TabPage一侧为资源中的每张图片生成一个按钮(用于测试的6张图片,最终构建128张)。

这是我到目前为止的位置:

private void tabPage1_load(object sender, EventArgs e)
{
    ResourceSet rs = new ResourceSet("");
    IDictionaryEnumerator id = rs.GetEnumerator();
    List<Bitmap> CIcons = new List<Bitmap>();
    while (id.MoveNext())
    {
        if (id.Value is Bitmap)
            CIcons.Add((Bitmap)id.Value);
    }
}

这似乎无法解决问题,任何建议将不胜感激

编辑(添加):问题是应用程序启动时,我看不到int“ tabPage1”中列出的图像。

也可以,我确实在Visual Studios中将6张图像添加到我的“资源文件夹”中。

为了将来的人们,我想添加完成的工作代码:

        // Button list start
        // Credit to Jcl
        ResourceSet rs = Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
        IDictionaryEnumerator id = rs.GetEnumerator();
        List<Bitmap> CIcons = new List<Bitmap>();
        while (id.MoveNext())
        {
            if (id.Value is Bitmap)
                CIcons.Add((Bitmap)id.Value);
        }


        int yposition = 0;
        foreach (var bmp in CIcons)
        {
            Button button = new Button();
            button.Location = new Point(0, yposition);
            button.Size = new Size(125, 125);
            button.Visible = true;
            button.BackgroundImage = bmp;
            tabPage1.Controls.Add(button);
            yposition += 125; 

        }
        //Button list end

如果您想生成一个按钮,我会说类似:

private void tabPage1_load(object sender, EventArgs e)
{
    ResourceSet rs = new ResourceSet("");
    IDictionaryEnumerator id = rs.GetEnumerator();
    List<Bitmap> CIcons = new List<Bitmap>();
    while (id.MoveNext())
    {
        if (id.Value is Bitmap)
            CIcons.Add((Bitmap)id.Value);
    }

    // Vertical aligned: i'll let you figure out how to position them
    int yposition = 0;
    foreach(var bmp in CIcons)
    {
       var button = new Button();   
       button.Location = new Point(0, yposition);
       button.Size = new Size(50, 20); // for example
       button.Visible = true;
       button.BackgroundImage = bmp;
       tabPage1.Controls.Add(button);        
       yposition += 20; // height of button

    }
}

更新资料

如注释中所述(我以为这是示例代码,但似乎不是),您还需要指定从何处获取ResourceSet 在您的情况下,请更改:

ResourceSet rs = new ResourceSet("");

对于

ResourceSet rs = Properties.Resources.ResourceManager.GetResourceSet(
                                  CultureInfo.CurrentUICulture, true, true);

代码易读性奖金

所有这些代码:

IDictionaryEnumerator id = rs.GetEnumerator();
List<Bitmap> CIcons = new List<Bitmap>();
while (id.MoveNext())
{
    if (id.Value is Bitmap)
        CIcons.Add((Bitmap)id.Value);
}

等效于:

List<Bitmap> CIcons = new List<Bitmap>();
foreach(var bmp in rs.OfType<Bitmap>())
  CIcons.Add(bmp);

并且由于您可以根据枚举创建列表,因此您只需执行以下操作:

List<Bitmap> CIcons = new List<Bitmap>(rs.OfType<Bitmap>());

而且,由于除了创建按钮之外,没有将位图列表用于其他任何事情,因此无法定义它,然后整个代码将变为:

var rs  = Properties.Resources.ResourceManager.GetResourceSet(
                         CultureInfo.CurrentUICulture, true, true);
int yposition = 0;
foreach (var bmp in rs.OfType<Bitmap>())
{
    var button = new Button()
    {
        Location = new Point(0, yposition),
        Size = new Size(125, 125),
        Visible = true,
        BackgroundImage = bmp,
    };
    tabPage1.Controls.Add(button);
    yposition += 125; 
}

可以进一步优化:如果我是您,则可以使用FlowLayoutPanel来排列按钮,而不是通过计算每个组件的像素位置进行定位。 不过, FlowLayoutPanel用法超出了此问题的范围,我只是在提及它,以防万一您想进一步调查并进一步谷歌搜索

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM