朱皮特的博客 自由的飞翔

在CDC上绘图保存为bmp等图片文件

2009-09-29
朱皮特
 
阅读量:

#include <atlimage.h>

CDC   *pdc   =   GetDC(); 
CImage   imag; 
CRect   rcClient; 
GetClientRect(   rcClient   ); 
imag.Create(   rcClient.Width(),rcClient.Height(),32); 
::BitBlt(imag.GetDC(),   0,0,rcClient.Width(),rcClient.Height(),   pdc-> m_hDC,0,0,SRCCOPY   ); 
imag.Save( "c:\\2.bmp "); 
ReleaseDC(   pdc); 
imag.ReleaseDC(); 
AfxMessageBox( "ok ");
CDC   *pdc   =   GetDC(); 
HBITMAP   hBitmap   =   NULL; 
if   (   pdc   ) 
{ 
/** 
*   创建位图段 
*/ 
BITMAPINFO   bmi; 
LPBYTE   pBits; 
ZeroMemory(&bmi,   sizeof(bmi)); 
CRect   rcClient; 
GetClientRect(   rcClient); 
bmi.bmiHeader.biSize   =   sizeof(BITMAPINFOHEADER); 
bmi.bmiHeader.biWidth   =   rcClient.Width(); 
bmi.bmiHeader.biHeight   =   rcClient.Height(); 
bmi.bmiHeader.biPlanes   =   1; 
bmi.bmiHeader.biBitCount   =   24; 
bmi.bmiHeader.biCompression   =   BI_RGB; 
hBitmap     =   CreateDIBSection(   pdc-> m_hDC,&bmi,DIB_RGB_COLORS,(void   **)&pBits,0,0   ); 

/** 
*   创建兼容dc并选择位图段 
*/ 
CDC   dcMem; 
dcMem.CreateCompatibleDC(   pdc); 
dcMem.SelectObject(   hBitmap     ); 

/** 
*   在内存dc上(即位图段上)绘图 
*   此函数你自己把你的绘制代码拿出来做成函数   void   Paint(   CDC   *pDC)就行了 
*/ 
dcMem.BitBlt(0,0,rcClient.Width(),rcClient.Height(),   pdc,0,0,SRCCOPY); 

/** 
*   释放dc 
*/ 
ReleaseDC(   pdc   ); 
} 


/** 
*   保存位图 
*/ 
if(   hBitmap   ) 
{ 
CImage   img; 
img.Attach(   hBitmap   ); 
img.Save(   "c:\\1.bmp "); 
DeleteObject(   hBitmap   );

Comments

Content