// if not palette provided,create one from DIB; if(!hpal) hpal=CreateDIBPalette(lpdibih); if(hpal) { holdpal=SelectPalette(hdc,hpal,true); RealizePalette(hdc); } // make sure to use the stretching mode for color picture; SetStretchBltMode(hdc,COLORONCOLOR); if(StretchDIBits(hdc,lpdcrect->left,lpdcrect->top,RECTWIDTH(lpdcrect),RECTHEIGHT(lpdcrect), 0,0,((LPBITMAPINFOHEADER)lpdibih)->biWidth,((LPBITMAPINFOHEADER)lpdibih)->biHeight, lpdibits,(LPBITMAPINFO)lpdibih,DIB_RGB_COLORS,SRCCOPY)) bsuccess=true; GlobalUnlock(hdib); if(holdpal) SelectPalette(hdc,holdpal,false); GlobalUnlock(hdib); return bsuccess; } LPBITMAPINFOHEADER lpbih; lpbih=(LPBITMAPINFOHEADER)GlobalLock(pDoc->m_hdib); RECT m_drect; RECT client; GetClientRect(&client); m_drect.left=(client.right-100)/2; m_drect.right=m_drect.left+100; m_drect.top=(client.bottom-100*((lpbih->biHeight)/(lpbih->biWidth)))/2; m_drect.bottom=m_drect.top+100*((lpbih->biHeight)/(lpbih->biWidth)); PaintDIB(pDC->GetSafeHdc(),&m_drect,pDoc->m_hdib,NULL,SRCCOPY);
不可以显示彩色图象。
{
HBITMAP hBmpOri = (HBITMAP) LoadImage(AfxGetInstanceHandle(), "D:\\Windows\\安装程序.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE); BITMAP bmpOrigin;
GetObject(hBmpOri, sizeof(BITMAP), &bmpOrigin);
BITMAPINFOHEADER BmpInfoHeader;
BYTE * pBits;
BmpInfoHeader.biSize = sizeof (BITMAPINFOHEADER) ;
BmpInfoHeader.biWidth = bmpOrigin.bmWidth ;
BmpInfoHeader.biHeight = bmpOrigin.bmHeight ;
BmpInfoHeader.biPlanes = 1 ;
BmpInfoHeader.biBitCount = 24 ;
BmpInfoHeader.biCompression = BI_RGB ;
BmpInfoHeader.biSizeImage = 0 ;
BmpInfoHeader.biXPelsPerMeter = 0 ;
BmpInfoHeader.biYPelsPerMeter = 0 ;
BmpInfoHeader.biClrUsed = 0 ;
BmpInfoHeader.biClrImportant = 0 ; HBITMAP hBmp = CreateDIBSection (NULL, (BITMAPINFO *) &BmpInfoHeader, 0, (void**)&pBits, NULL, 0) ;
int nResult = GetDIBits(pDC->GetSafeHdc(), hBmpOri, 0, bmpOrigin.bmHeight, pBits, (BITMAPINFO *) &BmpInfoHeader, DIB_RGB_COLORS);
StretchDIBits(pDC->GetSafeHdc(),5,5, 100,100, 0,0,bmpOrigin.bmWidth, bmpOrigin.bmHeight,
pBits, (BITMAPINFO*)&BmpInfoHeader, DIB_RGB_COLORS, SRCCOPY); DeleteObject(hBmp);
DeleteObject(hBmpOri);
}
{
LPBYTE lpdibih; // pointer to BITMAPINFOHEADER;
LPBYTE lpdibits; // pointer to DIB image bits;
// LPBITMAPINFOHEADER lp;
bool bsuccess;
HPALETTE holdpal=NULL; if(!hdib)
return NULL; lpdibih=(LPBYTE)GlobalLock(hdib);
lpdibits=lpdibih+sizeof(BITMAPINFOHEADER)+DIBNumColors(lpdibih)*sizeof(RGBQUAD);
// if not palette provided,create one from DIB;
if(!hpal)
hpal=CreateDIBPalette(lpdibih); if(hpal)
{
holdpal=SelectPalette(hdc,hpal,true);
RealizePalette(hdc);
} // make sure to use the stretching mode for color picture;
SetStretchBltMode(hdc,COLORONCOLOR); if(StretchDIBits(hdc,lpdcrect->left,lpdcrect->top,RECTWIDTH(lpdcrect),RECTHEIGHT(lpdcrect),
0,0,((LPBITMAPINFOHEADER)lpdibih)->biWidth,((LPBITMAPINFOHEADER)lpdibih)->biHeight,
lpdibits,(LPBITMAPINFO)lpdibih,DIB_RGB_COLORS,SRCCOPY))
bsuccess=true;
GlobalUnlock(hdib); if(holdpal)
SelectPalette(hdc,holdpal,false);
GlobalUnlock(hdib); return bsuccess;
}
LPBITMAPINFOHEADER lpbih;
lpbih=(LPBITMAPINFOHEADER)GlobalLock(pDoc->m_hdib);
RECT m_drect;
RECT client;
GetClientRect(&client);
m_drect.left=(client.right-100)/2;
m_drect.right=m_drect.left+100;
m_drect.top=(client.bottom-100*((lpbih->biHeight)/(lpbih->biWidth)))/2;
m_drect.bottom=m_drect.top+100*((lpbih->biHeight)/(lpbih->biWidth));
PaintDIB(pDC->GetSafeHdc(),&m_drect,pDoc->m_hdib,NULL,SRCCOPY);
你的程序似乎是参考DibLook这个例子,这例子可是久经考验的。
上面的程序应该没什么问题,看看你调入DIB的程序有没有问题。