Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Drawing a Transparent Text into a Bitmap

Posted on 2001-06-11
16
Medium Priority
?
522 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 400 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

704 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