2017-05-09 35 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)