Solved

DIB Resizing by VC/Win32 API

Posted on 2000-03-29
9
2,700 Views
Last Modified: 2012-06-27
I need to resize a DIB bitmap(not DDB!), and I have load DIB to memory and got the DIB handle and memory block, now I need to resize it in memory, For example, I have a DIB size is 100*100, then I want to change the size of it to 20*20, (not put to screen, I just need a new DIB handle which handle a 20*20 size DIB). I do this in Win98SE/VC++.  Could you give me some tips? Thanks a lot!
0
Comment
Question by:yuxiliu
  • 5
  • 4
9 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 2669044
You need to do this using a DDB, but you can do so without "loosing information".

You need to create a memory DC, then create a 32bit color memory bitmap of the desired final dimensions. Then use StretchDIBits to copy the DIB into the bitmap in the desired size.  Then use GetDIBits to retreive the results as a DIB.  

example followes.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2669089
int DstWdt = ?; // destination width.
int DstHgt = ?; // destination height.
int SrcWdt = ?; // source width.
int SrcHgt = ?; // source height.
void *SrcBitPtr = ?; // -> source bits;
void *DstBitPtr = ?; // -> dest bits;
BITMAPINFO SrcBmpInf = ?; // Source bitmap format.
BITMAPINFO DstBmpInf;  // destination bitmap format

// This is probably sufficient to set DstbmpInf for your needs.
// if the image size isn't being set to 0 (for a uncompressed RGB bitmap)
// then you will also need to set the size.
DstbmpInf = SrcBmpInf;
DstBmpInf.bmiHeader.biWidth = DstWdt;
DstBmpInf.bmiHeader.biHeight= DstHgt;

HDC MemDC = CreateCompatibleDC(NULL);
HBITMAP BmpHnd = CreateBitmap(DstWdt,DstHgt,1,32,NULL);
HBITMAP OldBmp = SelectObject(MemDC,BmpHnd);
StretchDIBits(MemDC,0,0,DstWdt,DstHgt,0,0,SrcWdt,SrcHgt,SrcBitPtr,&SrcBmpInf,DIB_RGB_COLORS,SRCCOPY);
GetDIBits(MemDC,BmpHnd,0,0,DstBitPtr,&DstBmpInf,DIB_RGB_COLORS);
SelectObject(MemDC,OldBmp);
DeleteObject(BmpHnd);
DeleteDC(MemDC);


Let me know if you have any questions.
0
 

Author Comment

by:yuxiliu
ID: 2671854
I have try your code, and the size now is OK but I can't see the picture inside. I think I give it a wrong imagesize. So, Could you tell me how to caculate image size? Befor I still not alloc memory for a new DIB image.
Thank you very much!
I need a 24bits
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:yuxiliu
ID: 2671869
I have try your code, and the size now is OK but I can't see the picture inside. I think I give it a wrong imagesize. So, Could you tell me how to caculate image size? Befor I still not alloc memory for a new DIB image.
Thank you very much!
I need a 24bits
0
 
LVL 22

Expert Comment

by:nietod
ID: 2672933
>> Could you tell me how to caculate image size?
Do you mean the actual size, like width and height?  I can't help you there, that depends on what you need.  Or do you mean the size in bytes for the pixel data?   That depends on the format you are using?  do you know what format you are using?

duh.  24bits. I missed that.

You would think it is the number of pixels times the number of bytes used to store a pixel (3 bytes in this case).  But that is not right, although it is close.     The problem is that the length of a single row of pxiels must be padded so it is a multiple of 4 bytes.  so for example, if you image was only 1 pixel wide, you would need 3 bytes in a row, but the row has to be padded to a multiple of 4, so you actually have to have 4 bytes per row.  Okay?  so you would do something like

int RowSize = Width*3

RowSiz += 3; // Add 3 to force the divide to round up.
RowSiz /= 4;  // Calculate number of multiples of 4.
RowSiz *= 4; // Calculate size.

int ImageSize = RowSiz*Height;

okay?
0
 

Author Comment

by:yuxiliu
ID: 2673904
It still not works, StretchDIBits() successful and return the number of scanlines, but GetDIBits() allways return 0, I try to use GetLastError(), it return 0(ERROR_SUCCESS);
why? Could you give me more tips?
0
 
LVL 22

Expert Comment

by:nietod
ID: 2674070
Post your code.
0
 

Author Comment

by:yuxiliu
ID: 2677680
These codes almost work now, some how, it still have some problem I don't know.
The color is not right, and some picture will have a offset on x or y direction, but some display good.
Is there still some problem with these code?

      //Get hdc for painting
      CDC* pDC = (GetDlgItem(IDC_DISPLAY_PANE))->GetDC();
      HDC hdc = pDC->GetSafeHdc();

      //Source DIB
      BITMAPINFO* pSrcBmpInfo;
      BITMAPINFOHEADER* pSrcBmpInfoHeader;
      BYTE* pSrcBits;
      long lSrcWidth, lSrcHeight;

      pSrcBmpInfo = (LPBITMAPINFO)GlobalLock(hDIB);
      pSrcBmpInfoHeader = (LPBITMAPINFOHEADER)pSrcBmpInfo;
      pSrcBits = (BYTE*)pSrcBmpInfo + sizeof(BITMAPINFOHEADER);
      lSrcWidth = pSrcBmpInfoHeader->biWidth;
      lSrcHeight = pSrcBmpInfoHeader->biHeight;
      
      //Destination DIB
      BITMAPINFO* pDstBmpInfo;
      BITMAPINFOHEADER* pDstBmpInfoHeader;
      BYTE* pDstBits;
      long lDstWidth, lDstHeight;
      long lDstImageSize;
      
      lDstHeight = 400;     //The size I want
      lDstWidth = 600;
      
      
      lDstImageSize = ((((lDstWidth * 24) + 31)& ~31) >> 3) * lDstHeight; //I Get From MSDN
      pDstBmpInfo = (LPBITMAPINFO)GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, lDstImageSize+sizeof(BITMAPINFO));
      pDstBmpInfoHeader = (LPBITMAPINFOHEADER)pDstBmpInfo;
      pDstBits =(BYTE*) pDstBmpInfo + sizeof(BITMAPINFOHEADER);
            
      //Before filling in Dst DIB info header copy palette
      memcpy(pDstBmpInfo, pSrcBmpInfo,sizeof(BITMAPINFOHEADER));      
      
      //Fill in Dst DIB info header
      pDstBmpInfoHeader->biHeight = lDstHeight;
      pDstBmpInfoHeader->biWidth = lDstWidth;

      pDstBmpInfoHeader->biBitCount = 24;
/*
      pDstBmpInfoHeader->biSize = sizeof(BITMAPINFO);
      pDstBmpInfoHeader->biSizeImage = lDstImageSize;
      pDstBmpInfoHeader->biXPelsPerMeter = 0;
      pDstBmpInfoHeader->biYPelsPerMeter = 0;
      pDstBmpInfoHeader->biCompression = BI_RGB;
      pDstBmpInfoHeader->biPlanes = 1;
*/
      
   
      //Prepare for resize
    HDC hdcMem;
    HBITMAP hbmMem, hbmOld, bmp;
      //bmp = ::CreateBitmap(lDstWidth, lDstHeight,1,24,NULL);
    hdcMem = CreateCompatibleDC(hdc);            //Create a memory DC
    hbmMem = CreateCompatibleBitmap(hdc, lDstWidth, lDstHeight);   //Create a memory Bitmap
      hbmOld = (HBITMAP)SelectObject(hdcMem, hbmMem);   //I think after this, all operation on hdcMem will put into hbmMem, am I right?

      //Stretch Src DIB to memory Bmp, Successful here I get return scan lines
      int ret = StretchDIBits(hdcMem, 0, 0, lDstWidth, lDstHeight, 0, 0, lSrcWidth, lSrcHeight, pSrcBits, pSrcBmpInfo, DIB_RGB_COLORS, SRCCOPY);

      //Get Bits from memory bitmap
      int x = GetDIBits(hdcMem, hbmMem, 0, lDstHeight, NULL, pDstBmpInfo, DIB_RGB_COLORS);
      x = GetDIBits(hdcMem, hbmMem, 0, lDstHeight, pDstBits, pDstBmpInfo, DIB_RGB_COLORS);
      int err = GetLastError();
      
      
      //Display to screen for testing
      x = ::SetDIBitsToDevice(
            hdc,              // handle to device context
            0,            // x-coordinate of upper-left corner of dest. rect.
            0,            // y-coordinate of upper-left corner of dest. rect.
            lDstWidth,        // source rectangle width
            lDstHeight,       // source rectangle height
            0,             // x-coordinate of lower-left corner of source rect.
            0,             // y-coordinate of lower-left corner of source rect.
            0,      // first scan line in array
            lDstHeight,      // number of scan lines
            pDstBits,  // address of array with DIB bits
            pDstBmpInfo,  // address of structure with bitmap info.
            DIB_RGB_COLORS       // RGB or palette indexes
            );
      


      GlobalUnlock(hDIB);
      GlobalFree(pDstBmpInfo);
0
 
LVL 22

Expert Comment

by:nietod
ID: 2677700
At the moment, I don't see anything wrong,b ut you are not clearning up at all, at least not int he portion of the code you posted.  You must seelcte the original bitmap back into the memory DC and you must delete the memory DC and bitmap.  That is very important!

What is hDIB?  how did you get it?

You say the colors are off, did you check the pixel data for the source bitmap to make sure that you are getting the right colors there?  If you are getting a pointer to the source pixel data incorrectly, then everythign after that will be wrong (could cause a shift in the image, could cause bad colors.)
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question