tags:

views:

548

answers:

1

How can I draw text (with setting font and size) on image and save it as JPEG?

For example

    CBitmap bitmap;
    bitmap.CreateBitmap(width, height, 1, 32, rgbData);

Here I want to draw some text on bitmap:

    CImage image;
    image.Attach(bitmap);

    image.Save(_T("C:\\test.bmp"), Gdiplus::ImageFormatJPEG);
+2  A: 
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);

Try this code snippet

ArsenMkrt
Thanks , and where i can set font and size?
Armen Khachatryan
lav ok, i find , many thanks!
Armen Khachatryan
AND WHEN I DRAW ARMENIAN TEXT < IT DRAWS ???? , why ? i set Courier AM font CFont font; font.CreatePointFont(80, "Courier AM"); memDC.SelectObject(font);
Armen Khachatryan
Use Unicode font, and Unicode characters, by the way... I am armenian too ;)
ArsenMkrt
ha tesa dra hamar hayeren greci lav , ok sax stacvav , Courier AM fontn ei ogtagorcum , voch unicode hayeren text greci ashxatec , Մերսի:
Armen Khachatryan
You are welcome!
ArsenMkrt