DIB Resizing by VC/Win32 API

Posted on 2000-03-29
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!
Question by:yuxiliu
  • 5
  • 4
LVL 22

Accepted Solution

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.
LVL 22

Expert Comment

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);

Let me know if you have any questions.

Author Comment

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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

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
LVL 22

Expert Comment

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;


Author Comment

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?
LVL 22

Expert Comment

ID: 2674070
Post your code.

Author Comment

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

LVL 22

Expert Comment

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.)

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Template syntax for variable length arrays 9 75
Using Diagnostic tools in VS2015: Unresoved allocations 19 105
Gaming Software 1 19
boost::uuid crashes 17 11
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

839 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