2016-04-15 63 views
0

首先是一些背景。我是一位相對較新的C#編程人員,並且已經通過幾個教程創建遊戲來學習它。我在Win8.1上運行VS2015社區。我正在學習最初編寫成在XNA下運行的遊戲開發教程。我正在使用Windows Forms創建關卡編輯器。到目前爲止,代碼正在以級別編輯器形式顯示而沒有問題。下一步,我遇到了問題,是將spritesheet加載到編輯器中,以便可以創建/修改級別。下面是加載spritesheet方法:給出參數無效錯誤的位圖函數

private void LoadImageList() 
    { 
     string filepath = Application.StartupPath + 
      @"\Content\PlatformTiles"; 
     Bitmap tileSheet = new Bitmap(filepath); 
     int tilecount = 0; 
     for (int y = 0; y < tileSheet.Height/TileMap.TileHeight; y++) 
     { 
      for (int x = 0; x < tileSheet.Width/TileMap.TileWidth; x++) 
      { 
       Bitmap newBitmap = tileSheet.Clone(new 
        System.Drawing.Rectangle(
         x * TileMap.TileWidth, 
         y * TileMap.TileHeight, 
         TileMap.TileWidth, 
         TileMap.TileHeight), 
         System.Drawing.Imaging.PixelFormat.DontCare); 

       imgListTiles.Images.Add(newBitmap); 
       string itemName = ""; 
       if (tilecount == 0) 
       { 
        itemName = "Empty"; 
       } 
       if (tilecount == 1) 
       { 
        itemName = "White"; 
       } 
       listTiles.Items.Add(new 
        ListViewItem(itemName, tilecount++)); 
      } 
     } 
    } 

    private void MapEditor_Load(object sender, EventArgs e) 
    { 
     LoadImageList(); 
    } 

    private void exitToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     game.Exit(); 
     Application.Exit(); 
    } 
    } 
} 

的spritesheet名爲PlatformTiles,位於項目的內容文件夾中的.png文件。該文件是使用Monogame的內容管理器加載的。當我構建項目時,Debug停止在將「filepath」變量分配給Bitmap的行。我得到了下面的屏幕截圖所示的錯誤消息:

enter image description here

我研究,就像我可以使用谷歌,MSDN,Monogame,和類似的網站。儘管還有其他類似的程序員獲取信息,但他們並沒有像我這樣的工作。所以我發佈了一些建議,告訴我可以採取什麼措施來解決問題。如果有任何問題,請告訴我。謝謝。

+0

正在傳遞在Monogame你不需要擴展。當您將資源加載到程序中時,Content Manager將分配一個內部擴展來管理資產文件。這與XNA將文件轉換爲xnb所做的相似。我已經用Monogame寫了VS或者4個遊戲程序,並且沒有任何問題離開了擴展。但是現在你提到它了,問題可能出在我以前沒有使用過位圖嗎?位圖是否需要擴展名來讀取字符串?我會嘗試一下,看看它是否有所作爲。謝謝! – Devbyskc

回答

1

你需要給擴展,以及當你在文件名

string filepath = Application.StartupPath + 
      @"\Content\PlatformTiles.png";