如何在图像上绘制文本(具有设置的字体和大小)并将其另存为JPEG?
例如
CBitmap bitmap;
bitmap.CreateBitmap(width, height, 1, 32, rgbData);
在这里,我想在位图上绘制一些文本:
CImage image;
image.Attach(bitmap);
image.Save(_T("C:\\test.bmp"), Gdiplus::ImageFormatJPEG);
最佳答案
CBitmap bitmap;
CBitmap *pOldBmp;
CDC MemDC;
CDC *pDC = GetDC();
MemDC.CreateCompatibleDC(pDC);
bitmap.CreateCompatibleBitmap(pDC, width, height );
pOldBmp = MemDC.SelectObject(&MyBmp);
CBrush brush;
brush.CreateSolidBrush(RGB(255,0,0));
CRect rect;
rect.SetRect (0,0,40,40);
MemDC.SelectObject(&brush);
MemDC.DrawText("Hello",6, &rect, DT_CENTER );
MemDC.SetTextColor(RGB(0,0,255));
GetDC()->BitBlt(0, 0, 50, 50, &MemDC, 0, 0, SRCCOPY);
//When done, than:
MemDC.SelectObject(pOldBmp);
ReleaseDC(&MemDC);
ReleaseDC(pDC);
bitmap.Save(_T("C:\\test.bmp"), Gdiplus::ImageFormatJPEG);
试试这个代码片段
关于C++ Windows位图绘制文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2309767/