Solved

Drawing a Transparent Text into a Bitmap

Posted on 2001-06-11
16
513 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
  • 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 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