简体   繁体   中英

Bitmap function giving parameter not valid error

First some background. I a relative new to C# programming and have been lear ning it by creating games following several tutorials. I am running VS2015 Community on Win8.1. I am following a game development tutorial that was originally written to be run under XNA. I am at the point where I am creating a level editor using Windows Forms. So far the code is working as the level editor form displays without problems. The next step, with which I am having the issue, is to load a spritesheet into the editor so levels can be created/modified. Here is the method to load the 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();
    }
  }
}

The spritesheet is a .png file named PlatformTiles and located in the Content folder for the project. The file was loaded using Monogame's Content manager. When I build the project, Debug stops at the line where the "filepath" variable is being assigned to Bitmap. I get the error message shown in the following screen shot:

在此处输入图片说明

I have researched as much as I could using Google, MSDN, Monogame, and similar sites. Though there are other similar instances of programmers getting the message, they are not working on things like I am. So I am posting to get advice on what I can do to fix the problem. If there are any questions please let me know. Thank you.

You need to give the extension as well when you're passing in the filename

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM