CsGL + Saving Image to Disk?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • illmatic

    #1

    CsGL + Saving Image to Disk?

    Can someone post or give me a link to C# code that will allow me to
    save what is currently being shown in my CsGL control to disk? Any
    format will do, though bmp, jpg, or gif is preferred.

    Thanks!
  • Quaternion

    #2
    Re: CsGL + Saving Image to Disk?

    illmatic wrote:
    [color=blue]
    > Can someone post or give me a link to C# code that will allow me to
    > save what is currently being shown in my CsGL control to disk? Any
    > format will do, though bmp, jpg, or gif is preferred.[/color]

    Bleh! Bleh!

    --
    Quaternion

    Comment

    • Michael Shafrir

      #3
      Re: CsGL + Saving Image to Disk?

      Bleh bleh? Can you please help me out?

      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Pieter Philippaerts

        #4
        Re: CsGL + Saving Image to Disk?

        "illmatic" <mshafrir@wam.u md.edu> wrote in message[color=blue]
        > Can someone post or give me a link to C# code that will allow me to
        > save what is currently being shown in my CsGL control to disk? Any
        > format will do, though bmp, jpg, or gif is preferred.[/color]

        OpenGL has a glReadPixels that you can use to get the pixels that are shown
        on the screen.
        I haven't tried it, but if you initialize a Bitmap instance with the right
        size and type [ie. RGBA] and then call the LockBits method, you may be able
        to pass the Scan0 pointer of the BitmapData class directly to the
        glReadPixels method as the pixels parameter. The only thing you have to do
        then is to unlock the bits and call the Save method to save your image in
        one of the supported formats.

        Regards,
        Pieter Philippaerts
        Managed SSL/TLS: http://www.mentalis.org/go.php?sl


        Comment

        • Pieter Philippaerts

          #5
          Re: CsGL + Saving Image to Disk?

          "Pieter Philippaerts" <Pieter@nospam. mentalis.org> wrote in message[color=blue]
          > OpenGL has a glReadPixels that you can use to get the pixels that are[/color]
          shown[color=blue]
          > on the screen.[/color]

          Here's some code that works, apart from the fact that the Bitmap is flipped:

          Bitmap b = new Bitmap(width, height, PixelFormat.For mat32bppArgb);
          BitmapData bd = b.LockBits(new Rectangle(0, 0, width, height),
          ImageLockMode.W riteOnly, PixelFormat.For mat32bppArgb);
          glReadPixels(0, 0, 1600, 1200, GL_BGRA_EXT, GL_UNSIGNED_BYT E, bd.Scan0);
          b.UnlockBits(bd );
          b.Save(@"c:\tes t.bmp", ImageFormat.Bmp );

          You can call the b.RotateFlip method to flip the bitmap before saving.

          Regards,
          Pieter Philippaerts
          Managed SSL/TLS: http://www.mentalis.org/go.php?sl

          P.S.: for more information about glReadPixels, check out
          http://msdn.microsoft.com/library/en...unc03_8m7n.asp


          Comment

          • Michael Shafrir

            #6
            Re: CsGL + Saving Image to Disk?

            I updated your code to follow CsGL syntax:

            public void saveScene()
            {
            Size s = Size;

            Bitmap b = new Bitmap(s.Width, s.Height,
            PixelFormat.For mat32bppArgb);
            BitmapData bd = b.LockBits(new Rectangle(0, 0, s.Width, s.Height),
            ImageLockMode.W riteOnly, PixelFormat.For mat32bppArgb);
            GL.glReadPixels (0, 0, 1600, 1200, GL.GL_BGRA_EXT,
            GL.GL_UNSIGNED_ BYTE, bd.Scan0);
            b.UnlockBits(bd );
            //b.RotateFlip();
            b.Save(@"c:\tes t.bmp", ImageFormat.Bmp );
            }

            However, when I run this code, my computer hard resets! It’s the
            strangest thing. Can you point out what I might be doing wrong?
            Thanks…


            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            • Pieter Philippaerts

              #7
              Re: CsGL + Saving Image to Disk?

              I hard coded the resolution while testing the code, but apparently I forgot
              to change one of the lines before posting it here.
              Try replacing the 1600 and 1200 in the call to glReadPixels with 'width' and
              'height' respectively.

              Regards,
              Pieter Philippaerts
              Managed SSL/TLS: http://www.mentalis.org/go.php?sl

              "Michael Shafrir" <mshafrir@wam.u md.edu> wrote in message
              news:emi3dQDnDH A.2488@TK2MSFTN GP12.phx.gbl...[color=blue]
              > I updated your code to follow CsGL syntax:
              >
              > public void saveScene()
              > {
              > Size s = Size;
              >
              > Bitmap b = new Bitmap(s.Width, s.Height,
              > PixelFormat.For mat32bppArgb);
              > BitmapData bd = b.LockBits(new Rectangle(0, 0, s.Width, s.Height),
              > ImageLockMode.W riteOnly, PixelFormat.For mat32bppArgb);
              > GL.glReadPixels (0, 0, 1600, 1200, GL.GL_BGRA_EXT,
              > GL.GL_UNSIGNED_ BYTE, bd.Scan0);
              > b.UnlockBits(bd );
              > //b.RotateFlip();
              > b.Save(@"c:\tes t.bmp", ImageFormat.Bmp );
              > }
              >
              > However, when I run this code, my computer hard resets! It's the
              > strangest thing. Can you point out what I might be doing wrong?
              > Thanks.[/color]


              Comment

              Working...