找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

帖子
查看: 2311|回復(fù): 0
收起左側(cè)

C++ 指針與引用

[復(fù)制鏈接]
ID:77367 發(fā)表于 2015-4-18 20:13 | 顯示全部樓層 |閱讀模式
傳遞一個(gè)指針變量的引用



// 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);


// 生成一個(gè)1bpp的圖像出來(lái)
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();// 只有一列的數(shù)據(jù),所以偏移量是0bit就夠了  2048
int  iHeight = pBitmap->GetHeight();// 簡(jiǎn)單的,原始圖像高度是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;
}



回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表