哪位大侠帮我看看,我显示的bmp图像为什么会失真??

hust_jiang 2003-08-20 07:09:50
这个是绘制函数,谢谢了!

///向一个窗口画一个BMP图象
long vrDrawBitmpToWnd(HWND hwnd,CString szBitmapName)
{
long lResult=S_OK;
CBitmap bitmap;
CPalette ppal;
CFile file;

if( !file.Open(szBitmapName, CFile::modeRead) )
return S_FALSE;

BITMAPFILEHEADER bmfheader;

// read file header
if (file.Read((LPSTR)&bmfheader, sizeof(bmfheader)) != sizeof(bmfheader))
return S_FALSE;
WORD TT;
TT=((WORD) ('M'<<8|'B'));
// file type should be 'bm'
if (bmfheader.bfType !=TT)
return S_FALSE; // get length of the remainder of the file and allocate memory
DWORD npackeddiblen=0;
DWORD filelen;
filelen=file.GetLength();
npackeddiblen=file.GetLength()-sizeof(BITMAPFILEHEADER);
HGLOBAL hdib=::GlobalAlloc(GMEM_FIXED, npackeddiblen);
if (hdib== 0) return S_FALSE; // read the remainder of the bitmap file.
// file.Seek(0,CFile::begin);
DWORD readlen=file.ReadHuge(hdib, npackeddiblen);
if(readlen!=npackeddiblen )
{
::GlobalFree(hdib);
return S_FALSE;
}
BITMAPINFOHEADER &bmiheader=*(LPBITMAPINFOHEADER)hdib ;
BITMAPINFO &bminfo=*(LPBITMAPINFO)hdib ; // if bmiheader.biclrused is zero we have to infer the number
// of colors from the number of bits used to specify it.
int ncolors=bmiheader.biClrUsed ? bmiheader.biClrUsed : 1 << bmiheader.biBitCount;
LPVOID lpdibbits;
if( bminfo.bmiHeader.biBitCount> 8 )
lpdibbits = (LPVOID)((LPDWORD)(bminfo.bmiColors +
bminfo.bmiHeader.biClrUsed) +
((bminfo.bmiHeader.biCompression == BI_BITFIELDS)
? 3 : 0));
else
lpdibbits = (LPVOID)(bminfo.bmiColors + ncolors);

// create the logical palette
if(& ppal != NULL )
{
// create the palette
if( ncolors <= 256 )
{
UINT nsize=sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * ncolors);
LOGPALETTE *plp=(LOGPALETTE *) new BYTE[nsize];
plp->palVersion = 0x300;
plp->palNumEntries = ncolors;

for( int i=0; i<ncolors;i++)
{
plp->palPalEntry[i].peRed = bminfo.bmiColors[i].rgbRed;
plp->palPalEntry[i].peGreen = bminfo.bmiColors[i].rgbGreen;
plp->palPalEntry[i].peBlue = bminfo.bmiColors[i].rgbBlue;
plp->palPalEntry[i].peFlags = 0;
}

ppal.CreatePalette( plp );

delete[] plp;
}
}
HDC hdc=::GetDC( hwnd );
CDC dc;
dc.Attach(hdc);
CPalette* poldpalette = NULL;
if( &ppal )
{
poldpalette = dc.SelectPalette(&ppal, false );
dc.RealizePalette();
}

HBITMAP hbmp =::CreateDIBitmap(
dc.m_hDC, // handle to device context
&bmiheader, // pointer to bitmap size and format data
CBM_INIT, // initialization flag
lpdibbits, // pointer to initialization data
&bminfo, // pointer to bitmap color-format data
DIB_RGB_COLORS); // color-data usage
bitmap.Attach(hbmp );

if( poldpalette )
dc.SelectPalette( poldpalette, false );

CDC memdc;
memdc.CreateCompatibleDC(&dc );
memdc.SelectObject( &bitmap );

// select and realize the palette
if(( &ppal != NULL) &&( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE ))
{
dc.SelectPalette(&ppal, false );
dc.RealizePalette();
}

BITMAP bm;
bitmap.GetBitmap( &bm );
CRect drect;
::GetClientRect(hwnd,&drect);

dc.StretchBlt(drect.left, drect.top, drect.Width(), drect.Height(), &memdc, 0, 0,bm.bmWidth,bm.bmHeight,SRCCOPY);
dc.Detach();
bitmap.Detach();
bitmap.DeleteObject();
::GlobalFree(hdib);
return S_OK;
}
...全文
51 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
mct1025 2003-08-21
  • 打赏
  • 举报
回复
在这一句之前
dc.StretchBlt(drect.left, drect.top, drect.Width(), drect.Height(), &memdc, 0, 0,bm.bmWidth,bm.bmHeight,SRCCOPY);
加入
dc.SetStretchBltMode(COLORONCOLOR);
试一试

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧