在bmp上添加字符2 分类: VC++ 2013-09-10 08:30 588人阅读 评论(0) 收藏
void CTextOnbmpDlg::OnButton2()
{
// TODO: Add your control notification handler code here
FILE *fp=fopen("d:\\temp\\pic16001200\\000.bmp", "rb");
if(fp == 0)
return ;
HDC hDC = ::GetDC(GetSafeHwnd());
HDC hMemDC = CreateCompatibleDC(hDC);
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;
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));
//设置字体
CFont font;
font.CreateFont(10,10,0,0,10,FALSE,FALSE,FALSE,ANSI_CHARSET,OUT_DEFAULT_PRECIS,OUT_DEFAULT_PRECIS,DEFAULT_QUALITY,FF_SWISS,"宋体");
SelectObject(hMemDC, font);
// 添加文字到指定位置
m_pMemDC->TextOut(100, 100, "hello");
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[] = "test2.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);
delete m_pMemBmp;
delete m_pMemDC;
delete[] pBmpBuf;
delete[] pTemp;
MessageBox("VOER!");
}
版权声明:本文为博主原创文章,未经博主允许不得转载。