From: beginnercsharp on
I want to add text on bitmap,the following is the implementation.
After adding text watermark, I find text watermark is added on the
bitmap, while the bitmap is complete dark.The bitmap I used is not a
dark image. That is to say,generating bitmap only contain
watermark,not including original bitmap information.I still don't find
the bug to the function and do hope someone could give me some
clues,thanks in advance.

//pBitmap: the original bitmap
//BitmapNew: the bitmap created after processed the original bitmap
//iSize: text font size
//strText: text stuff
//color: text color
void AddTextWatermark(CBitmap *pBitmap,CBitmap &BitmapNew,int
iSize,LPCSTR strText,COLORREF color)
{
CDC dcSrc,dcDst ;
int nWidth, nHeight ;

BITMAP pBitMap ;
pBitmap->GetBitmap(&pBitMap) ;

//FONT
CFont textFont;
textFont.CreatePointFont(iSize,strText);

nWidthOld = pBitMap.bmWidth ;
nHeightOld = pBitMap.bmHeight ;

// Create DC
dcSrc.CreateCompatibleDC((CDC*)NULL);
dcDst.CreateCompatibleDC((CDC*)NULL);

// Source Bitmap
dcSrc.SelectObject(pBitmap);

// New Bitmap
BitmapNew.CreateCompatibleBitmap(&dcSrc,nWidth,nHeight);

// add watermarker on Bitmap
dcDst.SelectObject(&BitmapNew);
dcDst.SelectObject(&textFont);
dcDst.SetTextColor(color);
dcDst.SetBkMode(TRANSPARENT);
dcDst.TextOut(70,8,strText); //Position:(10,15)

// Free Resource
dcSrc.DeleteDC() ;
dcDst.DeleteDC() ;
}
From: Jeff Partch on
I think the specific problem you're asking about is a result of your
function never copying the original bitmap to the new bitmap, but you really
need to rethink the whole thing: there's no parameter validation, no error
checking, hard-wired coordinates, failures to deselect objects, and what
seems to me to be a misguided division of responsibilities. And those calls
to DeleteDC at the end imply that you have never even glanced at the
documentation.

--
Jeff Partch

"beginnercsharp" <huzhou1238(a)yahoo.com.cn> wrote in message
news:83629459-f956-4295-92e6-0618cdcc0ffb(a)b5g2000pri.googlegroups.com...
>I want to add text on bitmap,the following is the implementation.
> After adding text watermark, I find text watermark is added on the
> bitmap, while the bitmap is complete dark.The bitmap I used is not a
> dark image. That is to say,generating bitmap only contain
> watermark,not including original bitmap information.I still don't find
> the bug to the function and do hope someone could give me some
> clues,thanks in advance.
>
> //pBitmap: the original bitmap
> //BitmapNew: the bitmap created after processed the original bitmap
> //iSize: text font size
> //strText: text stuff
> //color: text color
> void AddTextWatermark(CBitmap *pBitmap,CBitmap &BitmapNew,int
> iSize,LPCSTR strText,COLORREF color)
> {
> CDC dcSrc,dcDst ;
> int nWidth, nHeight ;
>
> BITMAP pBitMap ;
> pBitmap->GetBitmap(&pBitMap) ;
>
> //FONT
> CFont textFont;
> textFont.CreatePointFont(iSize,strText);
>
> nWidthOld = pBitMap.bmWidth ;
> nHeightOld = pBitMap.bmHeight ;
>
> // Create DC
> dcSrc.CreateCompatibleDC((CDC*)NULL);
> dcDst.CreateCompatibleDC((CDC*)NULL);
>
> // Source Bitmap
> dcSrc.SelectObject(pBitmap);
>
> // New Bitmap
> BitmapNew.CreateCompatibleBitmap(&dcSrc,nWidth,nHeight);
>
> // add watermarker on Bitmap
> dcDst.SelectObject(&BitmapNew);
> dcDst.SelectObject(&textFont);
> dcDst.SetTextColor(color);
> dcDst.SetBkMode(TRANSPARENT);
> dcDst.TextOut(70,8,strText); //Position:(10,15)
>
> // Free Resource
> dcSrc.DeleteDC() ;
> dcDst.DeleteDC() ;
> }


From: Joseph M. Newcomer on
See below...
On Sun, 4 May 2008 03:17:42 -0700 (PDT), beginnercsharp <huzhou1238(a)yahoo.com.cn> wrote:

>I want to add text on bitmap,the following is the implementation.
>After adding text watermark, I find text watermark is added on the
>bitmap, while the bitmap is complete dark.The bitmap I used is not a
>dark image. That is to say,generating bitmap only contain
>watermark,not including original bitmap information.I still don't find
>the bug to the function and do hope someone could give me some
>clues,thanks in advance.
>
>//pBitmap: the original bitmap
>//BitmapNew: the bitmap created after processed the original bitmap
>//iSize: text font size
>//strText: text stuff
>//color: text color
>void AddTextWatermark(CBitmap *pBitmap,CBitmap &BitmapNew,int
>iSize,LPCSTR strText,COLORREF color)
****
I'm curious why you are passing one bitmap as a pointer and the other as a reference?
Wouldn't it make more sense to pass both as reference?

You could also pass a const CString & for the font name
****
>{
> CDC dcSrc,dcDst ;
> int nWidth, nHeight ;
****
Try to avoid using commas in declaration lists. One variable, one line.
****
>
> BITMAP pBitMap ;
> pBitmap->GetBitmap(&pBitMap) ;
>
> //FONT
> CFont textFont;
> textFont.CreatePointFont(iSize,strText);
****
You are trusting this will work. Does it?
****
>
> nWidthOld = pBitMap.bmWidth ;
> nHeightOld = pBitMap.bmHeight ;
>
> // Create DC
> dcSrc.CreateCompatibleDC((CDC*)NULL);
> dcDst.CreateCompatibleDC((CDC*)NULL);
>
> // Source Bitmap
> dcSrc.SelectObject(pBitmap);
>
> // New Bitmap
> BitmapNew.CreateCompatibleBitmap(&dcSrc,nWidth,nHeight);
>
> // add watermarker on Bitmap
> dcDst.SelectObject(&BitmapNew);
> dcDst.SelectObject(&textFont);
> dcDst.SetTextColor(color);
> dcDst.SetBkMode(TRANSPARENT);
> dcDst.TextOut(70,8,strText); //Position:(10,15)
****
How in the world did you derive magical random numbers like 70, 8? They have no
significance, and are essentially noise. You need to derive these as functions of, say,
the actual font height, or width, or image size, or something like that. Otherwise, you
will have the wrong coordinates on any display but your own.

I notice that you are drawing the text into a bitmap, but the bitmap is uninitialized. You
did not copy the original bitmap from src to dest, so there is no reason to expect that
you will find it in the resulting destination bitmap.
****
>
> // Free Resource
> dcSrc.DeleteDC() ;
> dcDst.DeleteDC() ;
****
The above two lines are not needed; the ~CDC will take care of this.
****
>}
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm