char bmppath[]="1.bmp";
FILE *fp=fopen(bmppath,"rb");
if(fp==0)
return ;
fseek(fp,sizeof(BITMAPFILEHEADER),0);
BITMAPINFOHEADER head;
fread(&head,sizeof(BITMAPINFOHEADER),1,fp);
int bmpHeight=head.biHeight;
int bmpWidth=head.biWidth;
int biBitCount=head.biBitCount;
int lineByte=(bmpWidth*biBitCount/8+3)/4*4;
RGBQUAD *pColorTable;
if(biBitCount==8)
{
pColorTable=new RGBQUAD[256];
fread(pColorTable,sizeof(RGBQUAD),256,fp);
}
unsigned char *pBmpBuf=new unsigned char[lineByte*bmpHeight];
fread(pBmpBuf,1,lineByte*bmpHeight,fp);
CDC *m_pMemDC;
m_pMemDC = new CDC();
CDC *pDC=GetDC();
m_pMemDC->CreateCompatibleDC(pDC);
CBitmap *m_pOldBmp=NULL;
CBitmap *m_pMemBmp = new CBitmap(); //根据图片的大小创建一个兼容位图
m_pMemBmp->CreateCompatibleBitmap(pDC, bmpWidth,bmpHeight );
m_pOldBmp = m_pMemDC->SelectObject(m_pMemBmp);
//把图像的数据绘制到兼容位图上
SetDIBits(m_pMemDC->GetSafeHdc(), (HBITMAP)m_pMemBmp->m_hObject,
0, bmpHeight, (LPVOID)pBmpBuf, (BITMAPINFO*)&head, DIB_RGB_COLORS);
m_pMemDC->SetBkMode(TRANSPARENT);
m_pMemDC->SetTextColor(RGB(255, 0, 0));
m_pMemDC->TextOut(10,100,"adasd");
unsigned char *pTemp = new unsigned char[lineByte*bmpHeight];
GetDIBits(m_pMemDC->GetSafeHdc(), (HBITMAP)m_pMemBmp->m_hObject, 0, bmpHeight,
(LPVOID)pTemp, (BITMAPINFO*)&head, DIB_RGB_COLORS);
fclose(fp);
char bmpwrite[]="2.bmp";
fp=fopen(bmpwrite,"wb");
BITMAPFILEHEADER fileHead;
fileHead.bfType=0x4D42;
fileHead.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+lineByte*bmpHeight;
fileHead.bfReserved1=0;
fileHead.bfReserved2=0;
fileHead.bfOffBits=54;
fwrite(&fileHead,sizeof(BITMAPFILEHEADER),1,fp);
fwrite(&head,sizeof(BITMAPINFOHEADER),1,fp);
fwrite(pTemp,lineByte*bmpHeight,1,fp);
fclose(fp);