GDI+ - refresh too slow

Hi,

I have an application (managed c++) that works on two dimensional array of integers. Its size is user-defined, but let's assume it's 500x500. Each value represents different object - assume there are about 11 distinct values (0 - empty field and 1..10 - kinds of objects). I want to visualize it. So far I set up Graphics (g = this->CreateGraphics()) Pen (p) and SolidBrush (b) objects. Then I put loop
                         for(int i = 0; i < lattice->board->sizeX(); i++) {
                               for(int j = 0; j < lattice->board->sizeY(); j++) {
                                     if(lattice->board->getPlankton(i, j) != nullptr) {
                                          b->Color = lattice->board->getPlankton(i, j)->printColor();
                                          g->FillRectangle(b, offset_x + pix_size*i, offset_y + pix_size*j, pix_size, pix_size);
                                     }
                               }
                         }
Everything is neat and nice, as desired.

The problem is that this is simulation of integers in this array...when theirs quantity (!= 0) gets higher, refreshing image gets really slow. I can see how it goes column-by-column...and it's unacceptable for me.
I've already tried to set up double buffering with
                         this->SetStyle(static_cast<ControlStyles>(ControlStyles::DoubleBuffer |
                              ControlStyles::UserPaint |
                              ControlStyles::AllPaintingInWmPaint),
                              true);
                         this->UpdateStyles();
but this doesn't help me.

The questions are:
(1) what can I do to improve my performance?
or
(2) I do not insist on drawing it like that, I just need to have this array displayed in any way. Maybe there is better way to accompish that?

Thanks,
Grzegorz
LVL 14
GGuzdziolAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
I suggest you the following idea: create array of bytes with length 500*500*3. Every 3 bytes in this array are one pixel in (B, G, R) order. Then fill this array by any way you need, setting individual pixel colors. Function BitsToBitmapRGB24 converts such array to Bitmap. You can draw bitmap in the form, or show bitmap in pixturebox.
In my code sample, I create array and fill it by some way. For example, first 250 lines are blue, other 250 lines are red. Replace this with your own code.

    using namespace System::Runtime::InteropServices;
    using namespace System::Drawing::Imaging;
    ...

    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
             {
                 array<Byte>^ bytes = gcnew array<Byte>(500*500*3);

                 // Fill array by some way
                 int current = 0;
                 for ( int line = 0; line < 500; line++ )
                 {
                     for ( int column = 0; column < 500; column++ )
                     {
                         // Fill pixel [line, column]
                         if ( line < 250 )
                         {
                             // Blue
                             bytes[current++] = 255;
                             bytes[current++] = 0;
                             bytes[current++] = 0;
                         }
                         else
                         {
                             // red
                             bytes[current++] = 0;
                             bytes[current++] = 0;
                             bytes[current++] = 255;
                         }
                     }
                 }

                 Bitmap^ bmp = BitsToBitmapRGB24(bytes, 500, 500);
                 pictureBox1->Image = bmp;
             }

             // Create RGB24 bitmap from Byte array
             Bitmap^ BitsToBitmapRGB24(array<Byte>^ bytes, int width, int height)
             {
                 if (bytes->GetLength(0) < width * height * 3)
                 {
                     return nullptr;
                 }

                 Bitmap^ bmp = gcnew Bitmap(width, height, PixelFormat::Format24bppRgb);
                 int i;

                 BitmapData^ data = bmp->LockBits(Rectangle(0, 0, bmp->Width, bmp->Height),
                     ImageLockMode::WriteOnly, bmp->PixelFormat);


                 if (data->Stride == width * 3)
                 {
                     Marshal::Copy(bytes, 0, data->Scan0, width * height * 3);
                 }
                 else
                 {
                     for (i = 0; i < bmp->Height; i++)
                     {
                         IntPtr p(data->Scan0.ToInt32() + data->Stride * i);
                         Marshal::Copy(bytes, i * bmp->Width * 3, p, bmp->Width * 3);
                     }
                 }

                 bmp->UnlockBits(data);

                 return bmp;
             }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GGuzdziolAuthor Commented:
Perfectly, master! :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.