传递一个指针变量的引用
// function : String2BW
// description: generate a BW image(bpp==1) with a given string.
// param:
// pDst[out], Dst image data, ##NB## does not include bitmap head data
// size[out],Dst image size in Bytes
// strTitle[in], input string
// nWidth[in], image width in pixels
// nHeight[in], image height in pixels
// iFontSize[in], draw string font size
BOOL String2BW(BYTE*& pDst/*out*/, int& size/*out*/, CString strTitle, int nWidth, int nHeight, int iFontSize = 300)
{
ASSERT(nWidth > 0);
ASSERT(nHeight > 0);
if ( strTitle.IsEmpty() || nWidth <= 0 || nHeight <= 0)
{
return FALSE;
}
if (!pDst)
{
delete pDst;
pDst = NULL;
}
Bitmap *pBitmap = NULL;
RectF textArea;
StringFormat strFormat;
SolidBrush brText(Color::White);
// 生成一个1bpp的图像出来
pBitmap = new Bitmap(nWidth, nHeight, PixelFormat1bppIndexed);
ASSERT(pBitmap);
// ResetPalette(m_pBitmap);
//
Graphics *g = Graphics::FromImage(pBitmap);
g->Clear(Color::Black);
//g->SetInterpolationMode( InterpolationModeHighQualityBicubic );
strFormat.SetFormatFlags(StringFormatFlagsNoClip);
Gdiplus::Font textF(L"", (REAL)iFontSize, FontStyleRegular, UnitPoint, NULL);
g->MeasureString(strTitle,
strTitle.GetLength(),
&textF,
RectF(0, 0, 0, 0),
&textArea);
g->DrawString(strTitle,
strTitle.GetLength(),
&textF,
RectF(0,
0,
textArea.Width,
textArea.Height),
&strFormat,
&brText);
// paser data
int iWidth = pBitmap->GetWidth();// 只有一列的数据,所以偏移量是0bit就够了 2048
int iHeight = pBitmap->GetHeight();// 简单的,原始图像高度是384了。。。。
Gdiplus::PixelFormat format = pBitmap->GetPixelFormat();
ASSERT(iWidth == nWidth);
ASSERT(iHeight == nHeight);
ASSERT(format == PixelFormat1bppIndexed);
Rect rect(0, 0, nWidth, nHeight);
BitmapData *pBitmapData = new BitmapData;
pBitmap->LockBits(&rect, ImageLockModeRead, format, pBitmapData);
BYTE *pByte = (BYTE *)pBitmapData->Scan0;
//
size = pBitmapData->Stride * nHeight;
pDst = new BYTE[size];
ASSERT(pDst);
memcpy(pDst, pByte, size);
pBitmap->UnlockBits(pBitmapData);
//clear:
delete pBitmapData;
pBitmapData = NULL;
delete pBitmap;
delete g;
//return pBitmap;
return TRUE;
}
|