Click to See Complete Forum and Search --> : Resizing a buffer (GDI)
Orf
May 3rd, 2006, 05:36 AM
I need to resize a picture stored in a memory buffer. I'd like to use GDI to do that. I can resize and paint my picture to a memoryDC, but then I need to store the new picture in a new buffer... and I don't know how to do that (of course CDC::GetPixel isn't fast enough). Any hint?
Thanks and regards.
golanshahar
May 3rd, 2006, 11:39 AM
Look at:
::StretchDIBits() (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_1ppv.asp) to stretch the image.
::GetDIBits() (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_7gms.asp) to get the pixels from the dc.
Cheers
Orf
May 3rd, 2006, 04:51 PM
Golanshahar, I actually used ::StretchDIBits() to resize the image. At that point I have my image correctly drawn in the dc. GetDIBits() needs a bitmap as a parameter... should I use the compatible bitmap I used to initialize the memory DC?
And: GetDIBits copies the bits of the image in another buffer... maybe that will slow down my code a bit... Is there a way to access directly the bits in the CDC or it is considered illegal?
(Hope I managed to explain myself... a bit sleepy at the moment, sorry!)
golanshahar
May 4th, 2006, 03:55 AM
Golanshahar, I actually used ::StretchDIBits() to resize the image. At that point I have my image correctly drawn in the dc. GetDIBits() needs a bitmap as a parameter... should I use the compatible bitmap I used to initialize the memory DC?
Yes.
And: GetDIBits copies the bits of the image in another buffer... maybe that will slow down my code a bit... Is there a way to access directly the bits in the CDC or it is considered illegal?
(Hope I managed to explain myself... a bit sleepy at the moment, sorry!)
It wont slow it down, it will be much more faster than using GetPixel() ;)
Cheers
Orf
May 4th, 2006, 04:13 AM
Well, I'm sure the thing will be faster than using GetPixel :)
The point is in my app I have some buffers copied around (the source is a directshow filter) and I was trying to avoid one more copying step...
oh, well...
Now, I'm having trouble right now, so I try to post my code here:
BITMAPINFO binfoFrame;
binfoFrame.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
binfoFrame.bmiHeader.biPlanes = 1;
binfoFrame.bmiHeader.biBitCount = 32;
binfoFrame.bmiHeader.biCompression = BI_RGB;
binfoFrame.bmiHeader.biWidth = m_iFrameSourceWidth;
binfoFrame.bmiHeader.biHeight = m_iFrameSourceHeight;
//all other members set to 0, omitted here
CClientDC dc( this ); // "this" is a CDialog derived class
CDC dcMem;
dcMem.CreateCompatibleDC( &dc );
CBitmap bmpMem;
bmpMem.CreateCompatibleBitmap( &dc, m_iFrameDestWidth, m_iFrameDestHeight );
dcMem.SelectObject( bmpMem );
dcMem.SetStretchBltMode( COLORONCOLOR );
int iRes = StretchDIBits( dcMem.GetSafeHdc(),
0, 0, m_iFrameDestWidth, m_iFrameDestHeight,
0, 0, m_iFrameSourceWidth, m_iFrameSourceHeight,
pBuffer, &binfoFrame, DIB_RGB_COLORS, SRCCOPY );
int ibit = GetDIBits( dcMem, bmpMem, 0, m_iFrameDestHeight,
m_pFrameBuffer, &binfoFrame, DIB_RGB_COLORS );
DWORD err = GetLastError();
// ibit is 0, and the same for err!!!
BOOL bval = dc.BitBlt( 0, 0, m_iFrameDestWidth, m_iFrameDestHeight, &dcMem, 0, 0, SRCCOPY);
// but the blit to the screen DC works fine
...I can't manage to get the bits from the GetDIBits call... am I doing something dumb here?
Thanks and ciao.
Orf
May 4th, 2006, 04:48 AM
Well, I read in the documentation that GetDIBits only works if the bitmap involved in the process isn't selected in the DC...
I tried to deselect the bitmap using
dcMem.SelectObject( pbmpOld );
before calling GetDIBits, but nothing changes...
Orf
May 4th, 2006, 05:41 AM
...total sillines of myself!
I have to use one BITMAPINFO for the source and one for the destination!!!!
Now it works, thanks for your attention golanshahar.
Ciao!
golanshahar
May 4th, 2006, 07:03 AM
You are welcome, even though you managed to solve it yourself :D :wave:
Cheers
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.