2016-04-15 62 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";