2017-05-09 22 views
0

试图为资源文件夹中的图像生成文件名,并显示它屁股错误。 我创建文件名到字符串参数, 已经创建btn作为按钮'寻找“文件名”参数。 错误是:错误1'warCards.Properties.Resources'不包含'文件名'的定义。 游戏:战争(纸牌游戏) 感谢助理为图像构建文件名称作为资源C#

btn.Click += delegate(object sender, EventArgs e) 
      { 
       Random rnd = new Random(); 
       int num = rnd.Next(1, 14); 
       int letter = 0;// represent random cards symbol 1-dimond 2-heart 3-spades 4-clubs 
       string fileName = "_"; 
       if (num < 10 && num > 1) 
       { 
        fileName = fileName + "0" + num.ToString() + "_of_"; 
        if (isBlack) 
        { 
         letter = rnd.Next(1, 3); 
         if (letter == 1) 
         { 
          fileName += "spades"; 
         } 
         else 
         { 
          fileName += "clubs"; 
         } 
         //for example: try to create: _5_of_spades 
         btn.BackgroundImage = warCards.Properties.Resources.fileName; 
         btn.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; 
         btn.Enabled = false; 
         isBlack = !isBlack; 

        } 
       } 
       }; 
       this.Controls.Add(btn); 
       nextPos.X += 112; 

      }; 
+1

'int letter = 0;'lol ;-) –

+0

使用[''Assembly.GetEntryAssembly().GetManifestResourceNames()'](https://msdn.microsoft.com/en-us/library /system.reflection.assembly.getmanifestresourcenames(v=vs.110).aspx)检查资源的完整名称并使用['Assembly.GetEntryAssembly()。GetManifestResourceStream(string name)'](https:// msdn。 microsoft.com/en-us/library/xc4235zt(v=vs.110).aspx)来获取位图流,(你可以加载如果从一个流) –

+0

我应该使用这个命令? 。在资源文件夹上的图片是png –

回答

0

我做了它使用GetObject(字符串)方法。 谢谢大家

0

使用反射,你将能够检索从资源类型的属性。如果可以使用其他方法解决问题,则不应使用反射。只要是完整的,这是你能如何使用反射做到这一点:

 // Get a type reference of the Type containing the auto-generated resources from the .resx 
     var resourceType = typeof(Properties.Resources); 
     // Retrieve the information about the property containing the image 
     var propertyReference = resourceType.GetProperty("_5_of_spades", BindingFlags.Static | BindingFlags.NonPublic); 
     // Retrieve the value of the property 
     var propertyValue = propertyReference.GetValue(null) as Image; 

正如你可以使用清单资源,这些包含在标有生成操作“嵌入式资源”项目的所有文件中的注释中提到将其添加到Resources.resx文件中,而不是而不是。 (所选项目项目的属性窗格)。

如何读取嵌入式资源并将其存储到字典中的完整代码示例。

 // A dictionary to hold the images by their names 
     var images = new Dictionary<string, Image>(); 

     // Read the resources of the currently executing assembly 
     var assembly = Assembly.GetExecutingAssembly(); 
     // All the names of the files that have a "Build action = Embedded Resource" 
     var names = assembly.GetManifestResourceNames(); 
     foreach (var name in names) 
     { 
      // Check if it's the resource we want 
      if (name.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase)) 
      { 
       // Create a 'sanitized name' which excludes the "{AssemblyName}.Resources." and ".png" part 
       var prefix = $"{assembly.GetName().Name}.Resources.".Length; 
       var sanitizedName = name.Substring(prefix, name.IndexOf(".png") - prefix); 

       // Load the image from the Manifest Resource Stream 
       var image = Image.FromStream(assembly.GetManifestResourceStream(name)); 

       // Add the image to the dictionary 
       images.Add(sanitizedName, image); 
      } 
     } 

     // ... 

     // Retrieve the image from the dictionary by it's name 
     button1.BackgroundImage = images["_5_of_spades"]; 

     // ... 

     // If at any point forward you wish to unload the images from memory (Besides quiting the application) 
     button1.BackgroundImage = null; // Make sure no controls use the image anymore 
     Parallel.ForEach(images.Values, (image) => { image.Dispose(); }); // Cleanup in-memory images (Linq and TPL to make it a one-liner)