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!");
}
相关文章
- 【230310-4】已知圆心在x轴上的圆C与直线l:x+2倍根号2-10=0切于点E(m,2倍根号2),圆P:x2+(a+2)x+y2-ay+a+1=0 求: 1)c的方程 2)已知a>1,圆P与x轴
- 用键盘读入一个字符,然后在屏幕上显示一个用这个字符填充的对角线长5个字符,倾斜放置的菱形。
- 【230211-2】直角三角形FDE正好卡在边长为6的正方形ABCD中,E为BC中点,A在斜边FE上,角FDE=90度,求Rt三角形FDE的面积?
- 在ggplot2中,如何添加其他图例?
- 将栅格添加到ggmap基本地图:在ggplot2中设置alpha(透明度)并将颜色填充到inset_raster()
- 在ggplot2中在条之间添加空间
- 在Nexus 10/Android 4.2.2上缓慢的glTexSubImage2D性能(三星Exynos 5 w/ malit - t604)
- 可以在EAGLLayer上使用Quartz 2D绘图吗?
- 在cocos2d项目中添加android手机按返回键功能
- [cocos2d-x·分享]在cocos2d-x2.0上用shader实现灰度图