Solved

DIB Resizing by VC/Win32 API

Posted on 2000-03-29
9
2,696 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:yuxiliu
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> 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
Comment Utility
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
Comment Utility
Post your code.
0
 

Author Comment

by:yuxiliu
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

728 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now