gluScaleImage always return GL_OUT_OF_MEMORY while trying to resize image.

I'm trying to resize image by using OpenGL function gluScaleImage, writen with VC++ MFC Windows App. However, it always return GL_OUT_OF_MEMORY.
Here is my code:
CImage* ResizeImage(UINT width, UINT height, std::string& error){
CString fileName
CImage *_image;
HRESULT hResult = _image->Load(fileName);
if (FAILED(hResult)) {
    return false;
bool ret=false;
CBitmap *bmpObj = CBitmap::FromHandle(_image->operator HBITMAP());
BITMAP* bmpData = new BITMAP;
BYTE* outBuf = (BYTE*)malloc(width * height * 4 * sizeof(BYTE));;

int iError = gluScaleImage(GL_RGB, _image.GetWidth(), _image.GetHeight(), GL_BITMAP, bmpData->bmBits, width, height, GL_BITMAP, outBuf);
HBITMAP hBitmap = CreateBitmap(width, height, bmpData->bmPlanes, bmpData->bmBitsPixel, outBuf);
bmpObj = CBitmap::FromHandle(hBitmap);
return ret;

Anyone here can help me?
Who is Participating?
davebytesConnect With a Mentor Commented:
Few things:
1. Why are you using glu functions for this?  GDI can do this at least three or four different ways including stretchblt of bitmaps.  But for a longer solution, I found this discussion
2. have you tried adding a printf right before the call, and can you tell us what the source and dest width/height are?
3. have you tried other sizes, multiple images, verified the incoming data is valid, etc.?

Seems like you have no need to be using GLU functions (I avoid them whenever possible, they are NOT designed for performance, and in some cases not for accuracy either!).  If you are actually pushing stuff to GL, there may be other approaches to achieve your end goal -- if you tell us your end goal! ;)

Have you defined a rendering context?
huyntminhAuthor Commented:
I am new in Graphic programming as well as VS C++. :)
How can I define a rendering context?
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Well, in Win32 you generally use the wglCreateContext() and wglMakeCurrent() functions

HDC hDC = GetDC( hWnd );
hRC = wglCreateContext( hDC );
wglMakeCurrent( hDC, hRC );

And then to clean up:

wglMakeCurrent( NULL, NULL );
wglDeleteContext( hRC );

However, if you cannot use Win32, then I think you can do something like this:

dc = new CClientDC(this);
ogld.create(dc->m_hDC);//this creates the rendering context

where ogld is your OpenGLDevice instance.
hi huyntminh,

here is an example of gluScaleImage using glut:

you can download the glut-library here:

tell us, if you need help implementing the "read_texture"-func in the example or anything else ..

good luck :)


huyntminhAuthor Commented:
To InteractiveMind:
   I have tried as you said but gluScaleImage still return GL_OUT_OF_MEMORY value.
To ikework:
   I'm still looking at your samples. :) Please give me more time to reply you.
Thanks all.
huyntminhAuthor Commented:
Thanks davebytes. That's what I need so far.
Thanks all of you for answering this question.
Best regards.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.