Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 523
  • Last Modified:

Drawing a Transparent Text into a Bitmap

Hello,

Can you tell me how to draw a transparent Text into a Bitmap ?

TIA

Claud
0
Claud
Asked:
Claud
  • 6
  • 5
  • 3
  • +2
1 Solution
 
CesarioCommented:
Hi,

you can use the API Function SetBkMode:

// ***** Demo ****
procedure TForm1.Button1Click(Sender: TObject);
var
  OldBkMode: integer;
begin
  Image1.Picture.Bitmap.Canvas.Font.Color := clBlue;
  OldBkMode := SetBkMode(Image1.Picture.Bitmap.Canvas.Handle, TRANSPARENT);
  Image1.Picture.Bitmap.Canvas.TextOut(10, 10, 'Hello');
  SetBkMode(Image1.Picture.Bitmap.Canvas.Handle, OldBkMode);
end;

Best Regards

Cesario
0
 
kretzschmarCommented:
hi,

a sample from my paq
paints a specific line (can contain various fonts and colors) from a richedit to a specific position
of a bitmap


Procedure CopyLineFromRichEditToBitmap(R : TRichEdit;
                                       Line : Integer;
                                       B : TBitMap;
                                       AtPos : TPoint);
var
  I, C, MH, H, W : Integer;  //Counter, CharPos, MaxHeight, CharHeight, CharWidth
  TB, TB2 : TBitmap;         //Temporary Bitmaps
begin
  TB := TBitmap.Create;      //Create Temporary
  TB2 := TBitmap.Create;
  MH := 0;                   //Initialize
  H := 0;
  TB.Height := 100;
  TB.Width := 5;
  C := R.Perform(EM_LINEINDEX,Line,0);
  for I := 0 to Length(R.Lines[Line]) do  //Paint Line
  begin
    R.Selstart := C+I;
    R.SelLength := 1;
    TB.Canvas.Font.Assign(R.SelAttributes);  //Assign CharAttributes
    W := TB.Canvas.TextWidth(R.SelText);
    TB.Width := TB.Width + W;                //Expand Bitmap
    H := TB.Canvas.TextHeight(R.SelText);
    if MH < H then                           // MaxHeight
      MH := H;
    TB.Canvas.TextOut(TB.Width-W,50-h,R.SelText);  //Paint a Char
  end;
  TB2.Width := TB.Width - 5;  //adjust Painting Rect
  Tb2.Height := MH;
  TB2.Canvas.CopyRect(Rect(0,0,TB2.Width,TB2.Height),TB.Canvas,Rect(5,50-mh,tb.width,50));

  TB2.Transparent := True;   //Prepare
  TB2.TransparentMode := tmFixed;
  TB2.TransparentColor := clWhite;
  B.Canvas.Draw(AtPos.x,AtPos.Y,TB2);  //Paint to Original
  TB2.Free;  //Free Memory
  TB.Free;
end;

meikl ;-)
0
 
ITugayCommented:
Hi Claud,

here is it:

  with Image1.Picture do
  begin
    Bitmap.Canvas.Font.Name := 'Arial';
    Bitmap.Canvas.Font.Size := 16;

    Bitmap.Canvas.Brush.Style := bsClear; // here is the key!!!
    Bitmap.Canvas.TextOut(10,10,'Transparent text');
  end;


------
Igor.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
djadjaCommented:
Alternatively use the Windows API DrawText. It can centre text and handle & accelarators and much more - look it up the the Help...

eg,
DrawText(Bitmap.Canvas.Handle,PChar('&Test text'),-1,Rect(0,0,Bitmap.Width,Bitmap.Height),DT_CENTER);
0
 
kretzschmarCommented:
must this be an answer?
0
 
CesarioCommented:
Hi djadja,

you draw the Text but not transparent......
so change your answer to comment ;-)


Cesario
0
 
djadjaCommented:
>> kretzschmar
Errr, yes?! A question was asked "Can you tell me how to draw a transparent Text into a Bitmap ?" and several people have answered via comments but as per the tips link above which states "An answer is a specific solution to a question and should be submitted if it will solve the questioner's problem and doesn't duplicate a previous comment."

Hence I submitted an answer...It will solve his problem and it isn't a duplicate of someones comment. It is now up to Claud to accept it or not.

Your entry seems too long-winded (to me) just to explain drawing transparent text. It has lots of API calls and methods that have nothing to do with drawing text and will probably confuse Claud even more!

Sorry if I'm treading on anyones toes? - I was just trying to help the guy out!
0
 
djadjaCommented:
>> kretzschmar
Errr, yes?! A question was asked "Can you tell me how to draw a transparent Text into a Bitmap ?" and several people have answered via comments but as per the tips link above which states "An answer is a specific solution to a question and should be submitted if it will solve the questioner's problem and doesn't duplicate a previous comment."

Hence I submitted an answer...It will solve his problem and it isn't a duplicate of someones comment. It is now up to Claud to accept it or not.

Your entry seems too long-winded (to me) just to explain drawing transparent text. It has lots of API calls and methods that have nothing to do with drawing text and will probably confuse Claud even more!

Sorry if I'm treading on anyones toes? - I was just trying to help the guy out!
0
 
djadjaCommented:
Cesario...
I've used this before with various components...I suppose certain ones may not have the transparency set.

The old adage: "If you want something done properly do it yourself" may be used here...

SetBkMode(Bitmap.Canvas.Handle,TRANSPARENT);
DrawText(Bitmap.Canvas.Handle,PChar('&Test text'),-1,Rect(0,0,Bitmap.Width,Bitmap.Height),DT_CENTER);
0
 
djadjaCommented:
Sorry Cesario - just noticed you used SetBkMode yourself - my original point was to use the DrawText API as it has a lot more flexibility that just TextOut.

Claud -> give the points to Cesario - he gave you the quickest and easiest solution.
0
 
CesarioCommented:
djadja,

I agree with you that the DrawText function ist more flexible than Canvas.Textout..

Cesario

0
 
kretzschmarCommented:
my toes are red/blue,
you're a heavy person, djadja ;-)

(no offense meant)
0
 
CesarioCommented:
This is a greate chat room :-)
0
 
ClaudAuthor Commented:
sorry djadja, but as you said Cesario gave the first answer und works...
Next Time you have more luck...

Best Regards

Cesario
0
 
CesarioCommented:
Hi Claud,

thanx for the points. But I have a Q ??

You sign with my Name ?????????????????


Cesario
0
 
djadjaCommented:
No probs!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 6
  • 5
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now