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. C#
  4. Getting the DoubleBuffered Bitmap

Getting the DoubleBuffered Bitmap

Scheduled Pinned Locked Moved C#
graphicscsharptutorialquestion
12 Posts 3 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.
  • N Offline
    N Offline
    Neil Van Note
    wrote on last edited by
    #1

    While drawing in a double buffered control, how would one get a hold of the actual Bitmap that .NET is holding for the buffering? So you could set individual pixels for example. Aside from using yet another bitmap to do all of the drawing, which is what double buffering is doing in the first place. Thanks

    M J 2 Replies Last reply
    0
    • N Neil Van Note

      While drawing in a double buffered control, how would one get a hold of the actual Bitmap that .NET is holding for the buffering? So you could set individual pixels for example. Aside from using yet another bitmap to do all of the drawing, which is what double buffering is doing in the first place. Thanks

      M Offline
      M Offline
      Mazdak
      wrote on last edited by
      #2

      This is a double buffering code: (Its from Beginnning C# books by wrox publisher).Maybe it helps you:

      Graphics displayGraphics = e.Graphics;
      Random r = new Random();
      Image i = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
      Graphics g = Graphics.FromImage(i);
      g.FillRectangle(Brushes.White, ClientRectangle);

      for (int x = 0; x < ClientRectangle.Width; x++)
      {
      for (int y = 0; y < ClientRectangle.Height; y += 10)
      {
      Color c = Color.FromArgb (r.Next(255), r.Next(255),
      r.Next(255));
      Pen p = new Pen(c, 1);
      g.DrawLine(p, new Point(0, 0), new Point(x, y));
      p.Dispose();
      }
      }
      displayGraphics.DrawImage(i, ClientRectangle);
      i.Dispose();

      Mazy "The path you tread is narrow and the drop is shear and very high, The ravens all are watching from a vantage point near by, Apprehension creeping like a choo-train uo your spine, Will the tightrope reach the end;will the final cuplet rhyme?"Cymbaline-Pink Floyd

      N 1 Reply Last reply
      0
      • M Mazdak

        This is a double buffering code: (Its from Beginnning C# books by wrox publisher).Maybe it helps you:

        Graphics displayGraphics = e.Graphics;
        Random r = new Random();
        Image i = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
        Graphics g = Graphics.FromImage(i);
        g.FillRectangle(Brushes.White, ClientRectangle);

        for (int x = 0; x < ClientRectangle.Width; x++)
        {
        for (int y = 0; y < ClientRectangle.Height; y += 10)
        {
        Color c = Color.FromArgb (r.Next(255), r.Next(255),
        r.Next(255));
        Pen p = new Pen(c, 1);
        g.DrawLine(p, new Point(0, 0), new Point(x, y));
        p.Dispose();
        }
        }
        displayGraphics.DrawImage(i, ClientRectangle);
        i.Dispose();

        Mazy "The path you tread is narrow and the drop is shear and very high, The ravens all are watching from a vantage point near by, Apprehension creeping like a choo-train uo your spine, Will the tightrope reach the end;will the final cuplet rhyme?"Cymbaline-Pink Floyd

        N Offline
        N Offline
        Neil Van Note
        wrote on last edited by
        #3

        While I appreciate the answer, please read the question before answering it… ;-) The .NET Control already has the ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint styles set, which provides double buffering for the control. As a mater a fact, I am already doing something like what you posted (on top of double buffering) to get to any kind of SetPixel method. What I would like to know is if I can get a hold of the bitmap .NET already has, for the double buffering I told it to do? Thanks

        M 1 Reply Last reply
        0
        • N Neil Van Note

          While I appreciate the answer, please read the question before answering it… ;-) The .NET Control already has the ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint styles set, which provides double buffering for the control. As a mater a fact, I am already doing something like what you posted (on top of double buffering) to get to any kind of SetPixel method. What I would like to know is if I can get a hold of the bitmap .NET already has, for the double buffering I told it to do? Thanks

          M Offline
          M Offline
          Mazdak
          wrote on last edited by
          #4

          Neil Van Note wrote: While I appreciate the answer, please read the question before answering it… Ok,I'll do it.:) Neil Van Note wrote: The .NET Control already has the ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint styles set, which provides double buffering for the control. I didn't know this one,thanks. Mazy "The path you tread is narrow and the drop is shear and very high, The ravens all are watching from a vantage point near by, Apprehension creeping like a choo-train uo your spine, Will the tightrope reach the end;will the final cuplet rhyme?"Cymbaline-Pink Floyd

          1 Reply Last reply
          0
          • N Neil Van Note

            While drawing in a double buffered control, how would one get a hold of the actual Bitmap that .NET is holding for the buffering? So you could set individual pixels for example. Aside from using yet another bitmap to do all of the drawing, which is what double buffering is doing in the first place. Thanks

            J Offline
            J Offline
            James T Johnson
            wrote on last edited by
            #5

            I looked through the docs and nothing obvious stuck out at me. I even tried doing some p/invoke but got nowhere. It looks like you can't get the underlying bitmap unless you create it yourself :( James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971

            N 1 Reply Last reply
            0
            • J James T Johnson

              I looked through the docs and nothing obvious stuck out at me. I even tried doing some p/invoke but got nowhere. It looks like you can't get the underlying bitmap unless you create it yourself :( James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971

              N Offline
              N Offline
              Neil Van Note
              wrote on last edited by
              #6

              That’s what I found, I was hopping I was wrong... So this leads be to my second question. Is there a method that I am not seeing, that will allow me to set an individual pixel, other than those of the Bitmap’s? I am already creating a Bitmap to achieve this, I was just hopping there was a more direct method. Again, nothing in the documentation is jumping out at me. Thanks

              J 1 Reply Last reply
              0
              • N Neil Van Note

                That’s what I found, I was hopping I was wrong... So this leads be to my second question. Is there a method that I am not seeing, that will allow me to set an individual pixel, other than those of the Bitmap’s? I am already creating a Bitmap to achieve this, I was just hopping there was a more direct method. Again, nothing in the documentation is jumping out at me. Thanks

                J Offline
                J Offline
                James T Johnson
                wrote on last edited by
                #7

                Nothing directly unfortunately, you might be able to use DrawLine to simulate it. It really looks like GDI+ (ie the System.Drawing namespace) was meant for higher level operations. You can make a call out to gdi32.dll with p/invoke, but that could possibly be a real performance drag. I'll come up with a sample and compare the results of p/invoke and Bitmap.SetPixel :) James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971

                N 1 Reply Last reply
                0
                • J James T Johnson

                  Nothing directly unfortunately, you might be able to use DrawLine to simulate it. It really looks like GDI+ (ie the System.Drawing namespace) was meant for higher level operations. You can make a call out to gdi32.dll with p/invoke, but that could possibly be a real performance drag. I'll come up with a sample and compare the results of p/invoke and Bitmap.SetPixel :) James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971

                  N Offline
                  N Offline
                  Neil Van Note
                  wrote on last edited by
                  #8

                  >> "you might be able to use DrawLine to simulate it" :( Haaa, if it comes down to that, I'll stick with the Bitmap, the performance is actually pretty good considering I am keeping it around for the life of the control. P/Invoke can be a good and bad thing, the number of Sets are numerous and again, I would rather use the extra Bitmap, than to call out to SetPixelV 1000 times. I would be curious to see what your performance test show. Regards

                  J 1 Reply Last reply
                  0
                  • N Neil Van Note

                    >> "you might be able to use DrawLine to simulate it" :( Haaa, if it comes down to that, I'll stick with the Bitmap, the performance is actually pretty good considering I am keeping it around for the life of the control. P/Invoke can be a good and bad thing, the number of Sets are numerous and again, I would rather use the extra Bitmap, than to call out to SetPixelV 1000 times. I would be curious to see what your performance test show. Regards

                    J Offline
                    J Offline
                    James T Johnson
                    wrote on last edited by
                    #9

                    I've created a benchmark program, VS.NET project and batch file provided to compile it. GDIvsGraphics.zip James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971

                    N 1 Reply Last reply
                    0
                    • J James T Johnson

                      I've created a benchmark program, VS.NET project and batch file provided to compile it. GDIvsGraphics.zip James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971

                      N Offline
                      N Offline
                      Neil Van Note
                      wrote on last edited by
                      #10

                      Interesting results, while the internal Bitmap.SetPixel is still the clear winner, The PInvoke performance is not as bad as I thought is was going to be. Regards

                      J 1 Reply Last reply
                      0
                      • N Neil Van Note

                        Interesting results, while the internal Bitmap.SetPixel is still the clear winner, The PInvoke performance is not as bad as I thought is was going to be. Regards

                        J Offline
                        J Offline
                        James T Johnson
                        wrote on last edited by
                        #11

                        Whats even better is that the Graphics version is slowed down a little by calling clr.ToARGB() each iteration even though it isn't used. This was done so the comparison would be in using P/Invoke instead of P/Invoke and the needed call to ToARGB(). James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971

                        N 1 Reply Last reply
                        0
                        • J James T Johnson

                          Whats even better is that the Graphics version is slowed down a little by calling clr.ToARGB() each iteration even though it isn't used. This was done so the comparison would be in using P/Invoke instead of P/Invoke and the needed call to ToARGB(). James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971

                          N Offline
                          N Offline
                          Neil Van Note
                          wrote on last edited by
                          #12

                          True, Normally I wouldn't do such things in a benchmark; I feel benchmarks like this should only perform the work necessary to achieve the same end result, and not equivalent processing. I massaged the code a little and ran it under DevPartner Profiler to obtain (for my actual scenario) more accurate results. Thanks and Regards

                          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