Hi all! I'm new to pixel/graphic management and fairly new to C# as well but i'm learning some AI at the same time, very basic of course.
I did some kind of "Game of life", you know that it imitates cell birth and death and whatnot.

I have it working in a listview that i update a few times in a second so i can follow the simulation. I think i'm ready for the next step and i'm thinking on making a graphical representation of it instead of letters.

I'm probably implementing a 2D array, each cell will represent an actual cell for example like 0-death, 1-changing, 2-stablished...something like that. I think i want each cell to be represented by a pixel or maybe four pixels so it's not that tiny thing.

I just learnt a little bit about Bitmaps and how to draw them first and then put them on a picture box. I have this example:

System.Drawing.Bitmap flag = new System.Drawing.Bitmap(10, 10);
            for (int x = 0; x < flag.Height; ++x)
                for (int y = 0; y < flag.Width; ++y)
                    flag.SetPixel(x, y, Color.White);
            for (int x = 0; x < flag.Height; ++x)
                flag.SetPixel(x, x, Color.Red);
            pictureBox1.Image = flag;

Since i'm going to refresh that array and thus refreshing the bitmap, thus refreshing the picturebox, i wonder if there's a fastest/better way to do this.

On the other hand i want to know what would be the best way to convert those cells in the array into some icons that i can make. Instead of using coloured pixels.

Thank you!

PS: excuse my poor english.

Recommended Answers

All 8 Replies

I checked the Bob Powel tutorial but that didn't make my question more clear.
Im looking it up in google (bitmap lockbits) but not that good information, although i'll keep looking.

Do i need the picturebox or is it a better way? What about using little icons?

Thank you.

Why not use a panel instead and draw the cells in the panels paint event.
Sample code to draw a block of dots in panel1.

private void panel1_Paint(object sender, PaintEventArgs e)
{
    for (int i = 0; i < 100; i+=10)
    {
        for (int j = 0; j < 100; j+=10)
        {
            e.Graphics.FillEllipse(Brushes.Pink, new Rectangle(i, j, 10, 10));
        }
    }
}

Thanks for your reply, panel option seems interesting. But how can i draw and update (keep drawing) for example an ArrayList in a panel?
Do i have to call the paint event everytime like it was a refresh function?

How can i do it with icons that i can make in photoshop for example?

The Graphics class has many Draw and Fill functions for putting text, patterns, and images on to the control surface.
To get the panel (or any control) to re-paint you can either call Invalidate or Refresh. Generally, for container controls like the panel, you call Refresh as this also forces child controls to re-paint.

I modified my sample code to show the basics of putting an image on the control surface.
There are many more overrides of the DrawImage with lots of options.

int start = 0;
        int counter = 10;

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            for (int i = start; i < counter; i += 10)
            {
                for (int j = start; j < counter; j += 10)
                {
                    e.Graphics.DrawImage(Properties.Resources.Image1, new Point(i, j));
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            counter += 10;
            if (counter > 50)
                start += 10;
            panel1.Refresh();
        }

Image1 is a 10x10 BMP with a small circle drawn on it.
I set timer1 to tick every 1000ms.

I couldn't get it to work.
I didn't have any Properties.Resources.Image1, so i looked it up in google and saw that i had to create that.
Tried doing it by myself with Graphics class creating a circle but then your e.Graphics.DrawImage was expecting an Image not a Graphics so it didn't work.
Finally i draw it by myself with Paint (hahaha) saved it to a bmp and loaded to Visual Studio to the Properties.Resources

But now that i have it it doesn't appear anything, i get the timer ticking an everything but the form appears blank.

Anyways I see there's a lot to learn about Bitmap, Image and Graphic classes so not to be a pain in the ass i'll look them up and try to learn all i can about them.

There's something i need to know, if you were to draw the contents of a List, such as i explained in the first post, and refreshing it several times per second, would you use a Panel, a PictureBox or another thing?

And the second thing is the same question but using some hand made little icons instead of c# colors.

Thank you!

Your welcome.

But now that i have it it doesn't appear anything, i get the timer ticking an everything but the form appears blank.

Did you attach the panel1_Paint event handler to the panel on your form?

Finally i draw it by myself with Paint (hahaha) saved it to a bmp and loaded to Visual Studio to the Properties.Resources

VS has its own bitmap editor and can create bitmaps directly for use as resources.
On the properties resources tab select the drop-down for the Add Resource button and you'll find a New Image option.

There's something i need to know, if you were to draw the contents of a List, such as i explained in the first post, and refreshing it several times per second, would you use a Panel, a PictureBox or another thing?

Not sure about this one. Updating the user display once every 100ms is about the fastest you should probably go. The forms Timer only has a resolution of about 20ms anyway so you cannot go supper fast.

And the second thing is the same question but using some hand made little icons instead of c# colors.

The Graphics class has a couple of DrawIcon overrides.

Once you've had a read about the Graphics class etc. I'm sure you'll have some more specific questions. Just start a new thread and someone will help you.

Thank you, i forgot to attach the event name.
I got myself the head first c# 2nd edition so I may come later with hopefully clearer questions.

Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.