C# WinForms PictureBox控制如何提取加载到Resources.resx中的图像

C# WinForms PictureBox控制如何提取加载到Resources.resx中的图像,c#,visual-studio,visual-studio-2012,reflection,C#,Visual Studio,Visual Studio 2012,Reflection,我想知道如何使用WInForm PictureBox控件 我将简要描述我的应用程序是如何构建的,然后问我的问题 我使用的是C Visual Studio 2012,但是Visual Studio的版本可能并不重要 创建一个名为“FunWIthPictureBox”的新WinForms应用程序。 打开窗体的设计视图 添加PIctureBox控件并将其命名为pbxPicture 点击图片框。你会注意到在右上角有一个黑色箭头 单击黑色箭头以打开PictureBox任务对话框。单击“选择图像”选项 这将

我想知道如何使用WInForm PictureBox控件

我将简要描述我的应用程序是如何构建的,然后问我的问题

我使用的是C Visual Studio 2012,但是Visual Studio的版本可能并不重要

创建一个名为“FunWIthPictureBox”的新WinForms应用程序。 打开窗体的设计视图

添加PIctureBox控件并将其命名为pbxPicture

点击图片框。你会注意到在右上角有一个黑色箭头

单击黑色箭头以打开PictureBox任务对话框。单击“选择图像”选项

这将打开“选择资源”对话框。确保选中了“项目资源文件”单选按钮。单击“导入”按钮导入图像。完成后,再次单击“导入”并添加第二个图像。最后单击“确定”。我的图片名为“Picture1.jpg”和“Picture2.jpg”

现在,您已将两个位图图像加载到项目的资源文件Properties\Resources.resx中

双击表单以调出表单的加载事件

您可以使用以下代码将图片框的图像设置为Picture1.jpg… pbxPicture.Image=FunWIthPictureBox.Properties.Resources.Picture1; 请注意,“FunWithPictureBox”是名称空间

如果你想使用另一张图片,你可以像这样更新代码行。。 pbxPicture.Image=FunWIthPictureBox.Properties.Resources.Picture2

好的,到目前为止还不错。我试图找出如何提取已加载到.Properties.Resources中的所有图像,并将这些图像加载到图像列表中

通过使用反射和谷歌,我得出了以下结论:

使用System.Reflection添加

 private void PictureBoxForm_Load(object sender, EventArgs e)
    {
        pbxPicture.Image = FunWithPictureBox.Properties.Resources.Picture1;
        var properties = typeof(Properties.Resources).GetProperties(BindingFlags.NonPublic | BindingFlags.Static);
        foreach (var propertyinfo in properties)
        {
            if (propertyinfo.PropertyType.ToString() == "System.Drawing.Bitmap")
            {
                MessageBox.Show(propertyinfo.Name + " " + propertyinfo.PropertyType.ToString());
            }
        }
    }
这几乎给了我想要的。我能够识别资源文件中的图像

如何将这些图像添加到图像列表中?我有图像的名称,但我不知道如何访问图像并将其添加到列表中

像这样的

private void PictureBoxForm_Load(object sender, EventArgs e)
    {
        List<Image> lstImages = new List<Image>();
        pbxPicture.Image = FunWithPictureBox.Properties.Resources.Picture1;
        var properties = typeof(Properties.Resources).GetProperties(BindingFlags.NonPublic | BindingFlags.Static);
        foreach (var propertyinfo in properties)
        {
            if (propertyinfo.PropertyType.ToString() == "System.Drawing.Bitmap")
            {
                lstImages.Add(propertyinfo.Name); //This code does not wwork :-(
            }
        }
    }

有办法做到这一点吗

尝试将代码更改为:

        private void PictureBoxForm_Load ( object sender , EventArgs e ) {
            List<Image> lstImages = new List<Image> ( );
            //pbxPicture.Image = FunWithPictureBox.Properties.Resources.Picture1;
            var properties = typeof ( Properties.Resources ).GetProperties ( System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static );
            foreach ( var propertyinfo in properties ) {
                if ( propertyinfo.PropertyType.ToString ( ) == "System.Drawing.Bitmap" ) {
                    lstImages.Add ( ( Bitmap ) Properties.Resources.ResourceManager.GetObject ( propertyinfo.Name ) );
                }
            }
        }

Property.Resources有一个名为Resourcemanager的属性,这个属性有一个函数,用于使用对象的名称返回对象,以及如何使用属性info.name它可能返回位图。

这是一个非常详细的问题,只有一个答案。问题很清楚,问题显示了已经完成的前期工作。否决这个问题的人有心理问题。这位落选的选民可能没有多少朋友,我敢打赌他妈妈也不是真的爱他。