0

As I'm no expert using C#'s forms, I'm hoping I can get pointed in the right direction, as a particular section of code I have runs slow and i'm not sure how to speed it up.

The way I have it now, I have the user select a image and a tile size.

Based on the dimensions of the image and the selected tile size, I've been drawing images over top the main image. These images are the tile size with a number that fills the main images dimensions. I want them to act like buttons when I click on them. These images so far have been made as panels for this purpose.

It wouldn't be so bad if I didn't have to re calculate how many panels I need every time the user changes the image or the tile size. But as is, it chugs for about half a second, especially at lower tile sizes.

So in other words, since I have to make a lot of images/controls and display them dynamically, what would you recommend I do to be efficient about it?

What I have so far bellow.

    pictureBox.Controls.Clear();

    //Create our large list of buttons
    for (int y = 0; y < tileCountHeight; y++)
    {
        for (int x = 0; x < tileCountWidth; x++)
        {
            Panel newPicture = new Panel();
            int locX = x * tileSize;
            int locY = y * tileSize;
            newPicture.Location = new Point(locX, locY);

            //Determine the starting image
            int dataIndex = x + (y * tileCountWidth);

            if (passabilityList.Count >= dataIndex)
            {
                passabilityList.Add(Passability.Passable);
            }

            switch (passabilityList[dataIndex])
                {
                    case Passability.Blocked:
                        newPicture.BackgroundImage = SquareImage;
                        break;

                    case Passability.HideBehind:
                        newPicture.BackgroundImage = TriangleImage;
                        break;

                    case Passability.Passable:
                        newPicture.BackgroundImage = CircleImage;
                        break;
                }

                newPicture.Width = tileSize;
                newPicture.Height = tileSize;
                newPicture.Visible = true;
                newPicture.BackgroundImageLayout = ImageLayout.Center;
                newPicture.Name = "picOptionX" + x + "Y" + y;

                pictureBox.Controls.Add(newPicture);                            
                }
            } 
  • You'll have to throw this away. ListView with View = Tile is a decent way to display a lot of images. – Hans Passant Jul 09 '18 at 16:21
  • by "C# forms" I assume you are talking about Winforms? I would suggest you take a look into WPF instead as dynamically creating x number of buttons/controls/views is quite easy using a concept called DataTemplates which you could then apply to a ListView and bind to your list of objects. It depends whether you are willing to start your interface from scratch but I would recommend it for future development. – Paul Marshall Jul 09 '18 at 16:23
  • @Paul - That is correct, and I knew of WPF, but I know forms better and since I didn't think this would be that complicated of an application, I went with it – Jesse Finnegan Jul 09 '18 at 16:28
  • @Hans - Will I be able to determine if a image has been clicked on in a ListView? I assume I can, but want to make sure. – Jesse Finnegan Jul 09 '18 at 16:30
  • doing this with x * y controls creates a huge overhead. as a compromise, you could "ownerdraw" the content in the `Paint` event, there are [many useful examples](https://stackoverflow.com/q/22211048/1132334) on this site, involving the `DrawImage` method. You then need to do the hit testing yourself, but the maths for that is easy with only rectangular shapes. – Cee McSharpface Jul 09 '18 at 16:31
  • What are SquareImage, TriangleImage and CircleImage? The rest I've managed to hack around using: enum Passability{Blocked,HideBehind,Passable}; and var passabilityList = new List();var tileCountHeight = 10;var tileCountWidth = 10;var tileSize = 10; – Davesoft Jul 10 '18 at 15:00
  • After much playing, it's going to be difficult to run this at 60fps due to the overhead of them each being controls. Do they all really need to be controls or could the background be drawn at runtime then applied to the picturebox? – Davesoft Jul 10 '18 at 15:10

0 Answers0