   Problems initializing OpenGL when rendering to bitmap
  Hello everyone,I am using OpenGL between C# and C++ where the C++ DLL is handling all the OpenGL initialization and drawing. I am quite new to 3D programming and OpenGL.So, what I do is pass a bitmap from my managed C# code and I would like to render to this bitmap using OpenGL.Just to give a background, the relevant managed code is as follows:Graphics graphics = System.Drawing.Graphics.FromImage(_bitmap);IntPtr hDC = graphics.GetHdc();  try{    // If we have not initialized OpenGL, do it now.    if (!_isOpenGLInit)    {bool retval = NativeMethods.InitGL(out hDC, out this._hglrc);   _isOpenGLInit = true;    }}finally{    graphics.GetHdc(hDC);}Here _bitmap is the bitmap that I want to draw on and the format that GDI+ initializes it with is 32bit ARGB.Now, my openGL initialization code is as follows:extern "C" __declspec(dllexport) BOOL InitGL(HDC * hdc,   HGLRC * hglrc){  BOOL retval = FALSE;   PIXELFORMATDESCRIPTOR pfd;    int format;   ZeroMemory(&pfd, sizeof(pfd));    pfd.nSize = sizeof(pfd);    pfd.nVersion = 1;    pfd.dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL | PFD_SUPPORT_GDI;    pfd.iPixelType = PFD_TYPE_RGBA;    pfd.cColorBits = 24;    pfd.cDepthBits = 16;    pfd.iLayerType = PFD_MAIN_PLANE;   format = ChoosePixelFormat(*hdc, &pfd );    retval = SetPixelFormat(*hdc, format, &pfd); // this fails.    *hglrc = wglCreateContext(*hdc);   return retval;}The SetPixelFormat call fails and returns 0. ChoosePixelFormat returns a value of 104. GetLastError() returns ERROR_INVALID_FUNCTION.I am guessing that the pixelformat is somehow not possible with this bitmap. GDI+ does use 32bit ARGB while this OpenGL format that I could choose was RGBA. I wonder if that is a problem but GDI+ does not have RGBA and OpenGL does not seem to support ARGB! So, I am not able to test this theory...Besides that, it is rendering to a bitmap and I wonder if there is something special I need to do. However, I did not find anything on the net.I have spend a whole day trying to make this work with no luck. I would be really grateful if someone could help me with this.Many thanks,xarg   
  Does ChoosePixelFormat return a format? If yes, did you check that this format satisfies your needs?   ;  Hello,Thanks for the reply. Setting the colorBits to 32 seemed to have solved the problem and I see the image getting rendered now.I have one more problem. When I basically resize the bitmap, the subsequent rendering crashes with an access violation. I tried setting the viewport to the new size but this still happens. The application crashes with the error "Attempted to read or write to protected memory. This is often an indication of the other memory being corrupt".The drawing routine is very simple:extern "C" __declspec(dllexport) BOOL draw_gl(HDC *hdc, HGLRC *hglrc){  if (hdc && hglrc) {   wglMakeCurrent(*hdc, *hglrc);   glClearColor(0.0f, 0.0f, 0.0f, 0.0f);glClear(GL_COLOR_BUFFER_BIT);glPushMatrix();glRotatef(0.0f, 0.0f, 0.0f, 1.0f );glBegin(GL_TRIANGLES);glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(0.0f, 0.9f );glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.9f, -0.9f);glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(-0.9f, -0.9f);glEnd();glPopMatrix();   return TRUE;    }    return FALSE;}Any ideas what might be happening?Thanks,xarg   ;  Hello,I finally managed to solve it. It was because on resize the underlying bitmap was being recreated which meant I had to clean up OpenGL resources and reinitialize it with the new HDC. Now, it seems to work ok.Thanks,xarg   