保存字符串到图片

 

void CTestSaveStringDlg::OnBnClickedOk() { CFont font; LOGFONT lf; ZeroMemory(&lf,sizeof(LOGFONT)); lf.lfCharSet = 1; lf.lfHeight = -50; lf.lfWeight = 700; strcpy(lf.lfFaceName,"微软雅黑"); font.CreateFontIndirect(&lf); char txt[] = "abc1234字符串!!"; CRect rect(0,0,0,0); CPaintDC dc(this); //dc.SaveDC(); CDC bmpdc; bmpdc.CreateCompatibleDC(&dc); bmpdc.SaveDC(); bmpdc.SelectObject(font); //dc.SelectObject(&font); bmpdc.DrawText(txt, strlen(txt), rect, DT_CENTER | DT_VCENTER | DT_CALCRECT); CBitmap bmp; bmp.CreateCompatibleBitmap(&dc,rect.Width(),rect.Height()); bmpdc.SelectObject(bmp); bmpdc.FillSolidRect(&rect,RGB(255,255,255)); bmpdc.DrawText(txt,strlen(txt),rect,DT_CENTER | DT_VCENTER); SaveBmp((HBITMAP)bmp,".//aa.bmp"); bmpdc.RestoreDC(-1); bmpdc.DeleteDC(); } BOOL CTestSaveStringDlg::SaveBmp(HBITMAP bmp,char* path) { //把位图的信息保存到bmpinfo; BITMAP bmpinfo; GetObject(bmp,sizeof(BITMAP),&bmpinfo); DWORD dwBmBitsSize = ((bmpinfo.bmWidth * 32+31)/32) * 4 * bmpinfo.bmHeight; //位图文件头 14字节 BITMAPFILEHEADER bf; bf.bfType = 0x4D42; //BM bf.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwBmBitsSize; bf.bfReserved1 = 0; bf.bfReserved2 = 0; bf.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER); //位图信息头 BITMAPINFOHEADER bi; bi.biSize = sizeof(BITMAPINFOHEADER); bi.biWidth = bmpinfo.bmWidth; bi.biHeight = bmpinfo.bmHeight; bi.biPlanes = 1; bi.biBitCount = 32; bi.biCompression = BI_RGB; bi.biSizeImage = 0; bi.biXPelsPerMeter = 0; bi.biYPelsPerMeter = 0; bi.biClrUsed = 8; bi.biClrImportant = 0; //位图数据; char* context = new char[dwBmBitsSize]; HDC dc = ::GetDC(NULL); GetDIBits(dc, bmp, 0, bi.biHeight, context, (BITMAPINFO*)&bi, DIB_RGB_COLORS); FILE* f = fopen(path,"wb"); fwrite((char*)&bf,sizeof(BITMAPFILEHEADER),1,f);; fwrite((char*)&bi,sizeof(BITMAPINFOHEADER),1,f); fwrite(context,dwBmBitsSize,1,f); fclose(f); delete context; ::ReleaseDC(m_hWnd,dc); return 0; } 

 

你可能感兴趣的:(File,null,BI,delete,Path,colors)