A
Kurze Auflösung für alle Interessierten:
Bibliothek runterladen:
http://www.codeproject.com/bitmap/cximage.asp
Alternativer Code:
void GenerateBitmap ()
{
//Create a new image
CxImage image;
image.Create(640,480,24,CXIMAGE_FORMAT_BMP);
if (image.IsValid())
{
//Preparing
long width = image.GetWidth();
long height = image.GetHeight();
BITMAPINFO bmInfo;
memset(&bmInfo.bmiHeader,0,sizeof(BITMAPINFOHEADER));
bmInfo.bmiHeader.biSize =sizeof(BITMAPINFOHEADER);
bmInfo.bmiHeader.biWidth = width;
bmInfo.bmiHeader.biHeight = height;
bmInfo.bmiHeader.biPlanes = 1;
bmInfo.bmiHeader.biBitCount = 24;
BYTE *pbase; //points to the final dib
//get the background
HDC pDC = ::GetDC(0);
HDC TmpDC = CreateCompatibleDC(pDC);
HBITMAP TmpBmp = CreateDIBSection(pDC,&bmInfo,DIB_RGB_COLORS,(void**)&pbase,0,0);
HGDIOBJ TmpObj = SelectObject(TmpDC,TmpBmp);
//draw the background
image.Draw(TmpDC);
//choose the font
CFont m_Font;
LOGFONT* m_pLF;
m_pLF=(LOGFONT*)calloc(1,sizeof(LOGFONT));
strncpy(m_pLF->lfFaceName,"Times New Roman",31);
m_pLF->lfHeight=64;
m_pLF->lfWeight=600;
m_pLF->lfItalic=1;
m_pLF->lfUnderline=0;
m_Font.CreateFontIndirect(m_pLF);
//select the font in the dc
CDC dc;
dc.Attach(TmpDC);
CFont* pOldFont=NULL;
if (m_Font.m_hObject) pOldFont = dc.SelectObject(&m_Font);
else dc.SelectObject(GetStockObject(DEFAULT_GUI_FONT));
//Set text color
dc.SetTextColor(RGB(60,120,240));
//Set text position;
RECT pos = {40,40,0,0};
//draw the text
dc.SetBkMode(TRANSPARENT);
dc.DrawText("Test",4,&pos,DT_CALCRECT);
dc.DrawText("Test",4,&pos,0);
//cleanup
if (pOldFont) dc.SelectObject(pOldFont);
m_Font.DeleteObject();
dc.Detach();
free(m_pLF);
//transfer the result in the image
CxImage tmpimage;
tmpimage.CreateFromHBITMAP(TmpBmp);
tmpimage.DecreaseBpp(image.GetBpp(),0,image.GetPalette());
image.Transfer(tmpimage);
image.Save("tmp.bmp", CXIMAGE_FORMAT_BMP);
//cleanup
DeleteObject(SelectObject(TmpDC,TmpObj));
DeleteDC(TmpDC);
}
}