Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. .NET slow graphics

.NET slow graphics

Scheduled Pinned Locked Moved Managed C++/CLI
csharpwinformsgraphicsquestion
19 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 9 Offline
    9 Offline
    9ine
    wrote on last edited by
    #1

    Why is the .NET graphics in windows forms app is so slow, manualy ploting some N points line segments using double buffer takes considerable time compared to say builder compiled app? Is there special tips to make it faster.

    9ine

    L J 2 Replies Last reply
    0
    • 9 9ine

      Why is the .NET graphics in windows forms app is so slow, manualy ploting some N points line segments using double buffer takes considerable time compared to say builder compiled app? Is there special tips to make it faster.

      9ine

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      9ine wrote:

      ploting some N points line segments using double buffer

      Are you drawing into an Image and then displaying the Image on the Window?

      led mike

      9 1 Reply Last reply
      0
      • L led mike

        9ine wrote:

        ploting some N points line segments using double buffer

        Are you drawing into an Image and then displaying the Image on the Window?

        led mike

        9 Offline
        9 Offline
        9ine
        wrote on last edited by
        #3

        This is the whole code to render some float array to a form /////////data//////////////////////////////////////////////////////// Bitmap *canvas = new Bitmap(1280,1024); Graphics *memg = Graphics::FromImage(canvas); memg->SmoothingMode = SmoothingMode::HighSpeed; ///////////////////////////////////////////////////////////////////// private: System::Void p2Dform_Paint(System::Object *sender, System::Windows::Forms::PaintEventArgs *e) { paint(); } private: System::Void p2Dform_Resize(System::Object *sender, System::EventArgs *e) { resize(); } private: void p2Dform::paint(void) { Graphics *g = CreateGraphics(); System::Drawing::Rectangle rect(0,0,ClientSize.Width,ClientSize.Height); //draw image data g->DrawImage(canvas,0,0,rect,GraphicsUnit::Pixel); g->Dispose(); } private: void p2Dform::resize(void) { memg->FillRectangle(wbrush,0,0,ClientSize.Width,ClientSize.Height); //draw file data to memg draw(ClientSize.Width,ClientSize.Height,color,m_data,len,miny,maxy); paint(); } private: void p2Dform::draw(int width,int height,int color, float *data,int len,float minY,float maxY) { bpen = new Pen(Color::FromArgb(color)); float stepx,stepy,X1,Y1,X2,Y2; stepx = float(width-1 - addX*2)/float(len-1); stepy = float(height-1 - addY*2)/Math::Abs(float(maxY-minY)); for(int i=1; i 0) Y1 -= minY; Y1 = float(height-1) - Y1*stepy; Y2 = data[i]; if(minY < 0) Y2 += (-1.0f)*minY; if(minY > 0) Y2 -= minY; Y2 = float(height-1) - Y2*stepy; memg->DrawLine(bpen,X1+addX,Y1-addY,X2+addX,Y2-addY); } bpen->Dispose(); } 9ine

        L 1 Reply Last reply
        0
        • 9 9ine

          This is the whole code to render some float array to a form /////////data//////////////////////////////////////////////////////// Bitmap *canvas = new Bitmap(1280,1024); Graphics *memg = Graphics::FromImage(canvas); memg->SmoothingMode = SmoothingMode::HighSpeed; ///////////////////////////////////////////////////////////////////// private: System::Void p2Dform_Paint(System::Object *sender, System::Windows::Forms::PaintEventArgs *e) { paint(); } private: System::Void p2Dform_Resize(System::Object *sender, System::EventArgs *e) { resize(); } private: void p2Dform::paint(void) { Graphics *g = CreateGraphics(); System::Drawing::Rectangle rect(0,0,ClientSize.Width,ClientSize.Height); //draw image data g->DrawImage(canvas,0,0,rect,GraphicsUnit::Pixel); g->Dispose(); } private: void p2Dform::resize(void) { memg->FillRectangle(wbrush,0,0,ClientSize.Width,ClientSize.Height); //draw file data to memg draw(ClientSize.Width,ClientSize.Height,color,m_data,len,miny,maxy); paint(); } private: void p2Dform::draw(int width,int height,int color, float *data,int len,float minY,float maxY) { bpen = new Pen(Color::FromArgb(color)); float stepx,stepy,X1,Y1,X2,Y2; stepx = float(width-1 - addX*2)/float(len-1); stepy = float(height-1 - addY*2)/Math::Abs(float(maxY-minY)); for(int i=1; i 0) Y1 -= minY; Y1 = float(height-1) - Y1*stepy; Y2 = data[i]; if(minY < 0) Y2 += (-1.0f)*minY; if(minY > 0) Y2 -= minY; Y2 = float(height-1) - Y2*stepy; memg->DrawLine(bpen,X1+addX,Y1-addY,X2+addX,Y2-addY); } bpen->Dispose(); } 9ine

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          I can't follow the code. what does this do? Graphics *g = CreateGraphics(); I don't understand why you have this:

          /////////data////////////////////////////////////////////////////////
          Bitmap *canvas = new Bitmap(1280,1024);
          Graphics *memg = Graphics::FromImage(canvas);
          memg->SmoothingMode = SmoothingMode::HighSpeed;
          /////////////////////////////////////////////////////////////////////

          and then in paint() you have this: g->DrawImage(canvas,0,0,rect,GraphicsUnit::Pixel); So you are drawing the bitmap into the graphics object of the bitmap? :confused:

          led mike

          9 1 Reply Last reply
          0
          • L led mike

            I can't follow the code. what does this do? Graphics *g = CreateGraphics(); I don't understand why you have this:

            /////////data////////////////////////////////////////////////////////
            Bitmap *canvas = new Bitmap(1280,1024);
            Graphics *memg = Graphics::FromImage(canvas);
            memg->SmoothingMode = SmoothingMode::HighSpeed;
            /////////////////////////////////////////////////////////////////////

            and then in paint() you have this: g->DrawImage(canvas,0,0,rect,GraphicsUnit::Pixel); So you are drawing the bitmap into the graphics object of the bitmap? :confused:

            led mike

            9 Offline
            9 Offline
            9ine
            wrote on last edited by
            #5

            the float data needs to be drawn once if there is resize event in the form. the data is drawn to memg graphics object. from which it is drawn to a form if it is obscured and then released by another window. if the form itself resized the data plot needs to be redrawn to fit changed dimensions of the form. this is double buffer. the float data is put into memg object wich then is painted to a form, so we dont see flick due to clearing the form and ploting data directly to a form. Graphics *g = CreateGraphics(); creates graphics objects of the form in OnPaint event and the memg object is painted into it. paint() and resize() is just stubs for OnPaint() and OnResize() events

            9ine

            L 1 Reply Last reply
            0
            • 9 9ine

              the float data needs to be drawn once if there is resize event in the form. the data is drawn to memg graphics object. from which it is drawn to a form if it is obscured and then released by another window. if the form itself resized the data plot needs to be redrawn to fit changed dimensions of the form. this is double buffer. the float data is put into memg object wich then is painted to a form, so we dont see flick due to clearing the form and ploting data directly to a form. Graphics *g = CreateGraphics(); creates graphics objects of the form in OnPaint event and the memg object is painted into it. paint() and resize() is just stubs for OnPaint() and OnResize() events

              9ine

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #6

              Sorry, my fault I missed the g vs. memg variable. :-O Did you try only redrawing the Bitmap when the data changes and let DrawImage() scale the image to the size of the window? That way your plotting code would not execute during resizing of the window.

              led mike

              9 1 Reply Last reply
              0
              • L led mike

                Sorry, my fault I missed the g vs. memg variable. :-O Did you try only redrawing the Bitmap when the data changes and let DrawImage() scale the image to the size of the window? That way your plotting code would not execute during resizing of the window.

                led mike

                9 Offline
                9 Offline
                9ine
                wrote on last edited by
                #7

                it is not possible, we need to plot a resized version of the plot like in magnifying the 1D plot by means of incresing size of the canvas

                9ine

                L 1 Reply Last reply
                0
                • 9 9ine

                  Why is the .NET graphics in windows forms app is so slow, manualy ploting some N points line segments using double buffer takes considerable time compared to say builder compiled app? Is there special tips to make it faster.

                  9ine

                  J Offline
                  J Offline
                  Joe Woodbury
                  wrote on last edited by
                  #8

                  The short answer is the .NET GDI runs in a way which precludes parts of it from being accelerated by a graphics card. The second issue is the extensive marshalling that goes on between .NET and the Win32 GDI layer.

                  Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                  9 1 Reply Last reply
                  0
                  • J Joe Woodbury

                    The short answer is the .NET GDI runs in a way which precludes parts of it from being accelerated by a graphics card. The second issue is the extensive marshalling that goes on between .NET and the Win32 GDI layer.

                    Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                    9 Offline
                    9 Offline
                    9ine
                    wrote on last edited by
                    #9

                    Thats very silly, having so much of the product developed and failed to beat simple builder C++ application??? Consider even compilation times! I noticed that builder compiled app 1D plotting runs faster on modern video cards compared to simple ones from the last century. I think there is another reason is the Bitmap object itself. In builder you can ajust bitmap canvas properties by simply adjusting its size without deleting the bitmap! However in .NET Bitmap object it can be created only of the fixed size! and you can not change its dimensions during run-time. The possible sollution is to delet it and allocate a new one to fit the form during the resize method. But in __gc compiled resources you can not delete them :-) the garbage cleaner do it. So if rely on the garbage cleaner to delete all the allocated Bitmaps during form resizing events you end up without free mem :-) Very silly.

                    9ine

                    9 C J 3 Replies Last reply
                    0
                    • 9 9ine

                      Thats very silly, having so much of the product developed and failed to beat simple builder C++ application??? Consider even compilation times! I noticed that builder compiled app 1D plotting runs faster on modern video cards compared to simple ones from the last century. I think there is another reason is the Bitmap object itself. In builder you can ajust bitmap canvas properties by simply adjusting its size without deleting the bitmap! However in .NET Bitmap object it can be created only of the fixed size! and you can not change its dimensions during run-time. The possible sollution is to delet it and allocate a new one to fit the form during the resize method. But in __gc compiled resources you can not delete them :-) the garbage cleaner do it. So if rely on the garbage cleaner to delete all the allocated Bitmaps during form resizing events you end up without free mem :-) Very silly.

                      9ine

                      9 Offline
                      9 Offline
                      9ine
                      wrote on last edited by
                      #10

                      probably the best application of the VC is the console ones

                      9ine

                      1 Reply Last reply
                      0
                      • 9 9ine

                        Thats very silly, having so much of the product developed and failed to beat simple builder C++ application??? Consider even compilation times! I noticed that builder compiled app 1D plotting runs faster on modern video cards compared to simple ones from the last century. I think there is another reason is the Bitmap object itself. In builder you can ajust bitmap canvas properties by simply adjusting its size without deleting the bitmap! However in .NET Bitmap object it can be created only of the fixed size! and you can not change its dimensions during run-time. The possible sollution is to delet it and allocate a new one to fit the form during the resize method. But in __gc compiled resources you can not delete them :-) the garbage cleaner do it. So if rely on the garbage cleaner to delete all the allocated Bitmaps during form resizing events you end up without free mem :-) Very silly.

                        9ine

                        C Offline
                        C Offline
                        Christian Graus
                        wrote on last edited by
                        #11

                        9ine wrote:

                        In builder you can ajust bitmap canvas properties by simply adjusting its size without deleting the bitmap!

                        Some helper class is doing this, and probably deleting/recreating the bitmap in the background. Bitmaps are a fixed size, like any allocation of memory.

                        9ine wrote:

                        . But in __gc compiled resources you can not delete them

                        You can Dispose them, which is the same thing, the unmanaged resource is cleaned up.

                        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                        9 1 Reply Last reply
                        0
                        • 9 9ine

                          it is not possible, we need to plot a resized version of the plot like in magnifying the 1D plot by means of incresing size of the canvas

                          9ine

                          L Offline
                          L Offline
                          led mike
                          wrote on last edited by
                          #12

                          Sorry, I am not following that. Using DrawImage you can scale to whatever size you want.

                          led mike

                          1 Reply Last reply
                          0
                          • 9 9ine

                            Thats very silly, having so much of the product developed and failed to beat simple builder C++ application??? Consider even compilation times! I noticed that builder compiled app 1D plotting runs faster on modern video cards compared to simple ones from the last century. I think there is another reason is the Bitmap object itself. In builder you can ajust bitmap canvas properties by simply adjusting its size without deleting the bitmap! However in .NET Bitmap object it can be created only of the fixed size! and you can not change its dimensions during run-time. The possible sollution is to delet it and allocate a new one to fit the form during the resize method. But in __gc compiled resources you can not delete them :-) the garbage cleaner do it. So if rely on the garbage cleaner to delete all the allocated Bitmaps during form resizing events you end up without free mem :-) Very silly.

                            9ine

                            J Offline
                            J Offline
                            Joe Woodbury
                            wrote on last edited by
                            #13

                            I wonder if builder is using a DIB Section below the surface.

                            Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                            9 1 Reply Last reply
                            0
                            • C Christian Graus

                              9ine wrote:

                              In builder you can ajust bitmap canvas properties by simply adjusting its size without deleting the bitmap!

                              Some helper class is doing this, and probably deleting/recreating the bitmap in the background. Bitmaps are a fixed size, like any allocation of memory.

                              9ine wrote:

                              . But in __gc compiled resources you can not delete them

                              You can Dispose them, which is the same thing, the unmanaged resource is cleaned up.

                              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                              9 Offline
                              9 Offline
                              9ine
                              wrote on last edited by
                              #14

                              dispose is of no help. consider resizing a window it is about dozen resize messages and you end up with no free mem. it does not delete the Bitmap object immediatly once executed!? Thats the great disadvantage of .NET objects you can not resize them once allocated nor delete them :-) Simple Borland Builder posses all these features, the only disadvantage is its debugging process, VC provides more useful quickwatch.

                              9ine

                              C 1 Reply Last reply
                              0
                              • J Joe Woodbury

                                I wonder if builder is using a DIB Section below the surface.

                                Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                                9 Offline
                                9 Offline
                                9ine
                                wrote on last edited by
                                #15

                                What is it. and is it useful? The problem is to render promptly floating array of data to a window. Once you created bitmap in Builder you can alter its size without deleting it. So without a flick you can plot the data you want. .NET graphics is good for ploting image files not some floating array to fit a window. Probably the only solution to a lame .NET graphics is opengl one?

                                9ine

                                J 1 Reply Last reply
                                0
                                • 9 9ine

                                  dispose is of no help. consider resizing a window it is about dozen resize messages and you end up with no free mem. it does not delete the Bitmap object immediatly once executed!? Thats the great disadvantage of .NET objects you can not resize them once allocated nor delete them :-) Simple Borland Builder posses all these features, the only disadvantage is its debugging process, VC provides more useful quickwatch.

                                  9ine

                                  C Offline
                                  C Offline
                                  Christian Graus
                                  wrote on last edited by
                                  #16

                                  9ine wrote:

                                  consider resizing a window it is about dozen resize messages and you end up with no free mem. it does not delete the Bitmap object immediatly once executed!?

                                  Like I said, you can delete the bitmap every time, Dispose does this.

                                  9ine wrote:

                                  Thats the great disadvantage of .NET objects you can not resize them once allocated nor delete them

                                  NOR DOES BORLAND BUILDER !!!

                                  9ine wrote:

                                  Simple Borland Builder posses all these features

                                  You're just plain wrong. Borland cannot change the nature of Windows, it cannot create a world where you can resize a bitmap without deallocating and reallocating it. All it can do is hide the facts from you.

                                  Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                                  9 1 Reply Last reply
                                  0
                                  • C Christian Graus

                                    9ine wrote:

                                    consider resizing a window it is about dozen resize messages and you end up with no free mem. it does not delete the Bitmap object immediatly once executed!?

                                    Like I said, you can delete the bitmap every time, Dispose does this.

                                    9ine wrote:

                                    Thats the great disadvantage of .NET objects you can not resize them once allocated nor delete them

                                    NOR DOES BORLAND BUILDER !!!

                                    9ine wrote:

                                    Simple Borland Builder posses all these features

                                    You're just plain wrong. Borland cannot change the nature of Windows, it cannot create a world where you can resize a bitmap without deallocating and reallocating it. All it can do is hide the facts from you.

                                    Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                                    9 Offline
                                    9 Offline
                                    9ine
                                    wrote on last edited by
                                    #17

                                    then the builder is more succesfull in hiding windows facts from me than the native VC does. dispose results in delayed memory deallocation

                                    9ine

                                    C 1 Reply Last reply
                                    0
                                    • 9 9ine

                                      What is it. and is it useful? The problem is to render promptly floating array of data to a window. Once you created bitmap in Builder you can alter its size without deleting it. So without a flick you can plot the data you want. .NET graphics is good for ploting image files not some floating array to fit a window. Probably the only solution to a lame .NET graphics is opengl one?

                                      9ine

                                      J Offline
                                      J Offline
                                      Joe Woodbury
                                      wrote on last edited by
                                      #18

                                      http://www.3dsoftware.com/Programming/WindowsAPI/DIB_Section/[^] Realize that builder isn't just alerting the size; it is recreating bitmaps below the surface and copying the old data to the new one. Using DIB Sections you could do this copy fairly quickly. One solution is to write the code you want at the Win32 layer with C++/CLI. Another is to look around and see if any company is selling a toolkit that does what you want. I'd also suggest working with .NET some more and thoroughly understanding the graphics layer to make sure there isn't a great third solution I haven't thought of. (Oh, and you may need to rethink how you are doing your application. Perhaps a different algorithm on your part would make a purely .NET solution viable.)

                                      Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

                                      1 Reply Last reply
                                      0
                                      • 9 9ine

                                        then the builder is more succesfull in hiding windows facts from me than the native VC does. dispose results in delayed memory deallocation

                                        9ine

                                        C Offline
                                        C Offline
                                        Christian Graus
                                        wrote on last edited by
                                        #19

                                        Yes, that is precisely what a wrapper does. MFC doesn't wrap bitmaps in a way to hide these facts. It's no different to std::vector or CArray. Using those, you'd never guess that you can't create an array and then just keep adding stuff to it without it having to reallocate memory. The class hides this task from you.

                                        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups