HBITMAP hBm;
BITMAP bm;
hBm=LoadBitmap(hInstance,"forest");//forest是 位图
GetObject(hBc,sizeof(BITMAP).(LPVOID)&bm);
ShowWidow(hWnd,nCmdShow);
UpdateWindow(hWnd);
显示位图;
在API中:
BITMAP bm;
hBm=LoadBitmap(hInstance,"forest");//forest是 位图
GetObject(hBc,sizeof(BITMAP).(LPVOID)&bm);
ShowWidow(hWnd,nCmdShow);
UpdateWindow(hWnd);
显示位图;
在API中:
rect:是保存区域的大小。你可以不用。
void CPlanedrawView::BmpOut(CRect rect)
{
CClientDC dc(this);
OnPrepareDC(&dc);
CDC memdc;
memdc.CreateCompatibleDC(&dc);
memdc.SetMapMode(MM_LOMETRIC);
CBitmap cbmp;
CSize rectsize;
int Width,Height;
rectsize.cx=Width=rect.right-rect.left;
rectsize.cy=Height=rect.top-rect.bottom;
dc.LPtoDP(&rectsize);
TRACE("cx=%d,cy=%d\n",rectsize.cx,rectsize.cy);
if(!cbmp.CreateCompatibleBitmap(&dc,rectsize.cx,rectsize.cy))
{
TRACE("create bitmap error\n");
return;
}
CBitmap* poldbmp=memdc.SelectObject(&cbmp);
if(!memdc.BitBlt(0,-Height,Width,Height,
&dc,rect.left,rect.bottom,SRCCOPY))
{
TRACE("bitblt error\n");
return;
}
WORD cClrBits=(WORD)(dc.GetDeviceCaps(PLANES)*
dc.GetDeviceCaps(BITSPIXEL));
if(cClrBits==1)
cClrBits=1;
else if(cClrBits<=4)
cClrBits=4;
else if(cClrBits<=8)
cClrBits=8;
else if(cClrBits<=16)
cClrBits=16;
else if(cClrBits<=24)
cClrBits=24;
else
cClrBits=32;
BITMAPINFO* pbmi;
if(cClrBits!=24)
{
pbmi=(BITMAPINFO*)::LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER)+
sizeof(RGBQUAD)*(1<<cClrBits));
if(pbmi==NULL)
{
TRACE("create bitmapinfo error\n");
return;
}
}
else
{
pbmi=(BITMAPINFO*)::LocalAlloc(LPTR,sizeof(BITMAPINFOHEADER));
if(pbmi==NULL)
{
TRACE("create bitmapinfo error\n");
return;
}
}
BITMAPINFOHEADER* pbih=(BITMAPINFOHEADER*)&(pbmi->bmiHeader);
pbih->biSize=(DWORD)sizeof(BITMAPINFOHEADER);
pbih->biWidth=rectsize.cx;
pbih->biHeight=rectsize.cy;
pbih->biPlanes=1;
pbih->biBitCount=(WORD)dc.GetDeviceCaps(BITSPIXEL);
pbih->biClrImportant=0;
pbih->biCompression=BI_RGB;
pbih->biClrUsed=0;
pbih->biSizeImage=(((pbih->biWidth*pbih->biBitCount+31)
&~31)>>3)*pbih->biHeight;
TRACE("bitwidth is %d,height is %d,bitcount is %d,sizeimage is %d\n",pbih->biWidth,pbih->biHeight,pbih->biBitCount,pbih->biSizeImage);
WORD lpno=dc.GetDeviceCaps(SIZEPALETTE);
BYTE* pBits;
pBits=(BYTE*)LocalAlloc(LPTR,pbih->biSizeImage);
if(pBits==NULL)
{
TRACE("create pBits error\n");
}
memdc.SelectObject(poldbmp);
::GetDIBits(dc.GetSafeHdc(),(HBITMAP)cbmp,0,pbih->biHeight,
pBits,pbmi,DIB_RGB_COLORS);
BITMAPFILEHEADER* hdr;
hdr=(BITMAPFILEHEADER*)::LocalAlloc(LPTR,sizeof(BITMAPFILEHEADER));
hdr->bfType=0X4D42;
if(cClrBits!=24)
{
hdr->bfOffBits=(DWORD)(sizeof(BITMAPFILEHEADER)+pbih->biSize
+lpno*sizeof(RGBQUAD));
}
else
{
hdr->bfOffBits=(DWORD)sizeof(BITMAPFILEHEADER)+pbih->biSize;
}
hdr->bfSize=(DWORD)(hdr->bfOffBits+pbih->biSizeImage);
hdr->bfReserved1=0;
hdr->bfReserved2=0;
CFileDialog filedlg(FALSE,"","*.bmp",
OFN_OVERWRITEPROMPT,
"BMP FILES(*.bmp)|*.bmp");
CFile fl;
CFileException e;
if(filedlg.DoModal()==IDOK)
{
fl.Open(filedlg.GetPathName(),
CFile::modeWrite|CFile::modeCreate,&e);
fl.Write((void*)hdr,sizeof(BITMAPFILEHEADER));
if(cClrBits!=24)
{
fl.Write((void*)pbmi,sizeof(BITMAPINFOHEADER)+
lpno*sizeof(RGBQUAD));
}
else
{
fl.Write((void*)pbmi,sizeof(BITMAPINFOHEADER));
}
fl.Write((void*)pBits,pbih->biSizeImage);
fl.Close();
}
::LocalFree((HLOCAL)hdr);
::LocalFree((HLOCAL)pbmi);
::LocalFree((HLOCAL)pBits);}
然后选入dc
然后在dc上画图,
然后再选出来
这时候得到一个有图象的CBitmap然后是DDBtoDIB
再将DIB存储到bmp文件后面两步可以看msdn中的例子
MSDN\CD1\Samples\VC98\sdk\graphics\gdi\showdib\dib.c