2013-05-06 115 views

回答

2

你需要使用反射,像以下将做任务:

var properties = typeof(Properties.Resources).GetProperties 
    (BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); 

PictureBox[] array = new PictureBox[100]; 
int counter = 0; 
foreach (PropertyInfo property in properties) 
{ 
    var image = property.GetValue(null, null) as System.Drawing.Bitmap; 
    if (image != null && counter < array.Length) 
    { 
     array[counter] = new PictureBox(); 
     array[counter++].Image = image; 
    } 
} 

请记住,包括using System.Reflection;顶部。

2

你可以参考this的帖子。

,或者你可以简单地使用:

array[i].Image = Properties.Resources.img1 
+0

这是不是我问的 – 2013-05-06 11:47:35

4

如果您的图像有符合资源文件里面的一些模式(如“图像1”,“镜像2”等)的名称,你可以通过自己的名字载入它们:

ResourceManager rm = Resources.ResourceManager; 
array[i].Image = (Bitmap)rm.GetObject(string.Format("Image{0}", i)); 
+0

ResourceManager不能像这样使用。对不起,-1。想要给你使用ResourceManager的+1,但后来我看到你的代码暗示了一些不起作用的东西。无法再次将其设置为0。 – wonko79 2013-05-06 11:51:16

+0

@ wonko79:哎呀!丢失.GetObject调用。 – Artemix 2013-05-06 11:52:13

+0

啊!设置为+1后编辑:)有更新问题。 – wonko79 2013-05-06 11:55:10

1
namespace your_name_project 
{ 
    public partial class Form_Begin : Form 
    { 
     PictureBox[] pictureBoxs = new PictureBox[6]; 
     public Form_Begin() 
     { 
      InitializeComponent(); 
      pictureBoxs[0] = pictureBox1; pictureBoxs[1] = pictureBox2; pictureBoxs[2] = pictureBox3; 
      pictureBoxs[3] = pictureBox4; pictureBoxs[4] = pictureBox5; pictureBoxs[5] = pictureBox6;  
     } 
//continue 
     List<PictureBox> pictureBoxes = new List<PictureBox>(); 

      private void buttonX1_Click(object sender, EventArgs e) 
      { 
       for (int i = 0; i <3; i++) 
       { 
        pictureBoxs[i].Image =your_name_project.Properties.Resources.image_1;// load image1 and Image_2from resource in property of picturebox 
       } 
       for (int i = 3; i < 6; i++) 
       { 
        pictureBoxs[i].Image = your_name_project.Properties.Resources.Image_2; 
       } 
      } 
     } 
}