milky
March 25th, 2008, 07:59 AM
hi guys,
I'm have no idea how to add the system date and time to image in C code. Does anyone can help me on this?
CBasicNet
March 26th, 2008, 03:03 AM
You can use GDI+. Before you use GDI+, you need to initialize it.
#include <Gdiplus.h>
Gdiplus::GdiplusStartupInput m_gdiplusStartupInput;
ULONG_PTR m_gdiplusToken;
Gdiplus::GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);
Here is a function to do what you wants and saves the file
using namespace Gdiplus;
bool DateStampImage(
Image &SrcBmp,
const std::wstring& szDestFile,
const std::wstring& szEncoderString )
{
Image* pDestBmp = SrcBmp.Clone();
if( !pDestBmp )
return false;
Graphics graphics(pDestBmp);
time_t ltime;
memset(<ime, 0, sizeof(ltime));
time( <ime );
std::wstring szDateTime = _wctime( <ime );
Font myFont(L"Arial", 16, FontStyleBold);
PointF origin(1.0f, 1.0f);
SolidBrush blackBrush(Color(255, 0, 0, 0));
// Draw string.
graphics.DrawString( szDateTime.c_str(), szDateTime.length(),&myFont,origin,&blackBrush);
CLSID Clsid;
int result = GetEncoderClsid(szEncoderString.c_str(), &Clsid);
if( result < 0 )
return false;
Status status = pDestBmp->Save( szDestFile.c_str(), &Clsid );
delete pDestBmp;
pDestBmp = NULL;
return status == Ok;
}
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0)
return -1; // Failure
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return -1; // Failure
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j = 0; j < num; ++j)
{
if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j; // Success
}
}
free(pImageCodecInfo);
return -1; // Failure
}
Here is how you would typically call the function
using namespace Gdiplus;
Image SrcBmp(L"E:\\Media\\LYF\\lyf_1.jpg", TRUE);
bool bRet = DateStampImage(
SrcBmp,
L"E:\\Media\\LYF\\lyf_1_2.jpg",
L"image/jpeg" );
if( bRet )
MessageBoxW(NULL, L"Successful", L"Title", MB_OK );
else
MessageBoxW(NULL, L"Failed", L"Title", MB_OK );
Lastly, you need to shut down GDI+, after using.
Gdiplus::GdiplusShutdown(m_gdiplusToken);
milky
March 26th, 2008, 11:01 AM
hi CBasicNet,
thanks for the reply and the code. A doubt to ask. Does the code works in Linux? And the programming language u used is it a C# or C language?
Lindley
March 26th, 2008, 11:31 AM
GDI is a Windows thing. Won't work in Linux, probably.
There are packages out there for drawing text using OpenGL, but it's not as easy as you'd think it would be.