Solved

Drawing a Transparent Text into a Bitmap

Posted on 2001-06-11
16
516 Views
Last Modified: 2013-12-03
Hello,

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

TIA

Claud
0
Comment
Question by:Claud
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
  • +2
16 Comments
 
LVL 8

Accepted Solution

by:
Cesario earned 100 total points
ID: 6178489
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6180077
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
 
LVL 9

Expert Comment

by:ITugay
ID: 6180415
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!

 

Expert Comment

by:djadja
ID: 6180647
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6180671
must this be an answer?
0
 
LVL 8

Expert Comment

by:Cesario
ID: 6180718
Hi djadja,

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


Cesario
0
 

Expert Comment

by:djadja
ID: 6180725
>> 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
 

Expert Comment

by:djadja
ID: 6180735
>> 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
 

Expert Comment

by:djadja
ID: 6180742
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
 

Expert Comment

by:djadja
ID: 6180751
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
 
LVL 8

Expert Comment

by:Cesario
ID: 6180759
djadja,

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

Cesario

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6180806
my toes are red/blue,
you're a heavy person, djadja ;-)

(no offense meant)
0
 
LVL 8

Expert Comment

by:Cesario
ID: 6180826
This is a greate chat room :-)
0
 

Author Comment

by:Claud
ID: 6182157
sorry djadja, but as you said Cesario gave the first answer und works...
Next Time you have more luck...

Best Regards

Cesario
0
 
LVL 8

Expert Comment

by:Cesario
ID: 6182181
Hi Claud,

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

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


Cesario
0
 

Expert Comment

by:djadja
ID: 6182182
No probs!
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
error 1.1 400 Bad request idhttp delphi 18 285
URL for downloading Google Chrome for Win XP 2 284
can i put an image from resource into image tag ? 6 42
MS Access from Delphi 31 105
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question