Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6593137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:38:37+00:00 2026-05-25T17:38:37+00:00

Problem summary: I use OpenGL glReadPixels to get a screenshot but the buffer is

  • 0

Problem summary:

I use OpenGL glReadPixels to get a screenshot but the buffer is unchanged.
If I use glGetError to get the error from the last function, it returns 0, as if everything is fine.

I’ve researched online throughly and haven’t found anyone facing a problem as this one.

In details:

I’m using the CsGL.dll, which basically just wraps OpenGL to C#, and I have made the required initializations for using GL:
DC from hWnd, ChoosePixelFormat for DC using PixelFormatDescriptor and setting the it as it’s format, creating RC for the DC and calling wglMakeCurrent(RC, DC).


Note that I am using simple wrappers for the Windows API functions.
Here is the code for this initialization I have used:

public unsafe void Init(IntPtr hWnd)
{
        this.DC = (IntPtr)User.GetDC(this.HWnd = hWnd);

        var pfd = new PIXELFORMATDESCRIPTOR();
        var sizeOf = Marshal.SizeOf(pfd);

        Kernel.ZeroMemory(new IntPtr(&pfd), sizeOf);
        pfd.nSize = (short)sizeOf;
        pfd.nVersion = 1;
        pfd.dwFlags = (int)(PixelFormatDescriptorFlagsEnum.PFD_DRAW_TO_WINDOW |
                            PixelFormatDescriptorFlagsEnum.PFD_SUPPORT_OPENGL |
                            PixelFormatDescriptorFlagsEnum.PFD_DOUBLEBUFFER);
        pfd.iPixelType = PIXELFORMATDESCRIPTOR.PFD_TYPE_RGBA;
        pfd.cColorBits = 24;
        pfd.cDepthBits = 16;
        pfd.iLayerType = PIXELFORMATDESCRIPTOR.PFD_MAIN_PLANE;

        var iFormat = GDI.ChoosePixelFormat(this.DC, ref pfd);
        GDI.SetPixelFormat(this.DC, iFormat, ref pfd);

        this.RC = wglCreateContext(this.DC);

        wglMakeCurrent(this.DC, this.RC);
}

I’ve sent User.GetForegroundWindow() as the hWnd.

After this initialization I try getting a screenshot into in image (and I have also tried to read it into a simple byte array)

Short psuedo code of glReadPixels usage:

var area = new Rectangle(0, 0, 100, 100);
var bmp = new Bitmap(area.Width, area.Height);
var data = bmp.LockBits(area, ILM.WriteOnly, PF.24bppRgb);

glReadBuffer(BACK);
glReadPixels(0, 0, area.Width, area.Height, BGR_EXT /*also tried RGB and RGBA*/, UNSIGNED_BYTE, data.Scan0);

bmp.UnlockBits(data);
bmp.Save(@"C:\Back.bmp");

data = bmp.LockBits(area, ILM.WriteOnly, PF.24bppRgb);

glReadBuffer(FRONT);
glReadPixels(0, 0, area.Width, area.Height, BGR_EXT /*also tried RGB and RGBA*/, UNSIGNED_BYTE, data.Scan0);

bmp.UnlockBits(data);
bmp.Save(@"C:\Front.bmp");

Trying it with a simple and small byte[] was done like this:

var bytes = new byte[10 * 10 * 3];
glReadPixels(0, 0, 10, 10, RGB, UNSIGNED_BYTE, bytes);

And the byte[] was all zero’s.
I’ve also tried it with bigger size for the array (keeping the 0, 0, 10, 10), but still to no avail.


In both cases, the result is the same.
The buffer doesn’t change at all, while a glGetError call returns 0 after each GL function call.

Both Back.bmp and Front.bmp are totally black.

Please tell me what am I doing wrong?
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:38:38+00:00Added an answer on May 25, 2026 at 5:38 pm

    Are you trying to create a screenshot of something you rendered, or a screenshot of the screen in general?

    glReadPixels is assured to work only for contents of the OpenGL framebuffer. It’s not suited for taking screenshots of anything else (there used to be a time, where this was possible, but ever since the advent of compositing window managers those times are gone).

    So if you’re trying to do general screen capture, glReadPixels is the wrong way to go.

    If however you’re trying to take a screenshot of what you rendered, then you must make sure that the context you rendered with is also active when calling glReadPixels.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Summary of the problem: For some decimal values, when we convert the type from
Post-Answer-Acceptance Summary: The problem was the use of a pointer to a stack variable
You can't use wildcards in menu paths? A quick summary of my problem (which
we have a problem to use TransactionScope. TransactionScope get to us very good flexibility
The title attempts to summarize the problem, but here's a more detailed summary: our
i have this really weired js problem. in summary, i get a element not
I am facing problem in retrieving long value from PostgreSQL I use the following
In summary, I am having a problem where I read what I expect to
Here's my problem: there's an internal issue tracking system that has a nice summary
I am trying to write documentation comments however I have a problem. /// <summary>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.