Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I run my code, sometimes the form will be painted and sometimes it will just get stuck.
So I ran it in debug mode and I found that the first loop doesn't loop!
The loops ran 65025 total number of times so it has nothing to do with my PC.

C#
//frm is System.Windows.Forms.Form
for (byte z = 0; z < 255; z++)
            {
                for (byte x = 0; x < 255; x++)
                {
                    for (byte y = 0; y < 255; y++)
                    {
                        Color c = Color.FromArgb(z, x, y);
                        frm.CreateGraphics().FillRectangle(new SolidBrush(c), x, y, 1,    1);
                        frm.Refresh();
                        frm.Show();
                    }
                }
            }

Any ideas?
Posted
Comments
Suvendu Shekhar Giri 27-Mar-15 8:20am    
Have tried to find the issue by putting a try...catch.. block ? May be you can trap the actual issue in this way.
Sinisa Hajnal 27-Mar-15 8:29am    
- Put try..catch around it to see if there is an error
- log values of variables in each loop and see when it fails
- call refresh after show or don't call it at all

You do realize that you are trying to create 16,481,375 worth of new graphics contexts? And that these are a scarce resource that you are responsible for Disposing? And that if you don't Dispose of them properly, they will sit there until the Garbage collector decides to dispose of them? And if you run out of graphics contexts before the GC engages (which you will, around the 64K point) your application will crash?

And even if you didn't do that, the rectangles your draw are very, very transitory: the first time your form redraws, they will all disappear?

"The loops ran 65025 total number of times so it has nothing to do with my PC."
No. It has to do with your code on the PC.

Don't do that - any of it. :laugh:
 
Share this answer
 
Everything OriginalGriff said is true. Your loop is fine but since you are creating graphics everytime you paint a pixel you are draining resources (and slowing down your loop considerably). You also call refresh entire form after drawing every single pixel. You make your code performing very, very slow.

Just modify your code slighlty. Create graphisc only once and do not refresh 16 million times. You should see your colored squares fine.


C#
using (Graphics gr = this.CreateGraphics())
{

    for (byte z = 128; z < 255; z++)
    {
        for (byte x = 0; x < 255; x++)
        {
            for (byte y = 0; y < 255; y++)
            {
                Color c = Color.FromArgb(z, x, y);

                gr.FillRectangle(new SolidBrush(c), x, y, 1, 1);
            }
        }

       this.Refresh();
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900