Writing transparent text on an image

How can I write translucent text on an image (Jpg, Bmp) or transparent text (color as the same background image), but with a shadow, is something that I want to do for the watermark of the images.

I want to do this with Delphi win32.

+6
image-processing delphi
source share
6 answers

I assume that you are trying to perform a little harder than just writing text with a transparent background; those. you are trying to get some form of alpha mixed text written in an image.
The easiest way is to use the GDI + routines. They are encapsulated for delphi and are available for download from http://www.progdigy.com/ . There are many examples here that should be used as an example.

+3
source share

One option is to use the AlphaBlend function in the Windows.pas module. Something like this will create a translucent text (with shadow shadow - based on Jim MacKett's answer) superimposed on the image:

uses Windows, Graphics; . . . var BackgroundImage: Graphics.TBitmap; { need to call out specifically for Graphics.TBitmap because the Windows unit also has a TBitmap declaration } TextImage: Graphics.TBitmap; BlendFunc: BLENDFUNCTION; begin BlendFunc.BlendOp := AC_SRC_OVER; BlendFunc.BlendFlags := 0; BlendFunc.SourceConstantAlpha := $C0; { a hex value from $00-$FF (0-255). Represents the percent of opaqueness: $00 is completely transparent, $FF is completely opaque. $C0 is 75% opaque } BlendFunc.AlphaFormat := AC_SRC_ALPHA; { BackgroundImage is for holding the image you want to overlay text onto } BackgroundImage := Graphics.TBitmap.Create; try BackgroundImage.LoadFromFile('yourimagehere.bmp'); { Create another TBitmap to hold the text you want to overlay } TextImage := Graphics.TBitmap.Create; try { Set this bitmap to have the same dimensions as the background image you want the text to appear on. } TextImage.Height := BackgroundImage.Height; TextImage.Width := BackgroundImage.Width; { In my limited experience with AlphaBlend, Black is always 100% transparent. So, paint TextImage completely Black. Play around with this to see the effect it has on the final outcome. } TextImage.Canvas.Brush.Color := clBlack; TextImage.Canvas.FloodFill(0, 0, clNone, fsBorder); TextImage.Canvas.Font.Style := [fsBold]; { Write the shadow first } TextImage.Canvas.Brush.Style := bsClear; TextImage.Canvas.Font.Color := clDkGray; TextImage.Canvas.TextOut(11, 11, 'Test'); { Then put the text on top (slightly offset) } TextImage.Canvas.Brush.Style := bsClear; TextImage.Canvas.Font.Color := clMaroon; TextImage.Canvas.TextOut(10, 10, 'Test'); { Use the AlphaBlend function to overlay the bitmap holding the text on top of the bitmap holding the original image. } Windows.AlphaBlend(BackgroundImage.Canvas.Handle, 0, 0, TextImage.Width, TextImage.Height, TextImage.Canvas.Handle, 0, 0, TextImage.Width, TextImage.Height, BlendFunc); { Assign the now updated BackgroundImage to a TImage control for display } Image1.Picture.Bitmap.Assign(BackgroundImage); finally TextImage.Free; end; finally BackgroundImage.Free; end; end; 
+6
source share

The shadow is simple:

 // Bold shows up better when over an image image1.Canvas.Font.Style := [fsBold]; // Write the shadow first image1.Canvas.Brush.Style:=bsClear; image1.Canvas.Font.Color := clGrayText; image1.Canvas.TextOut(1, 1, 'hi there'); // Then put the text on top (slightly offset) image1.Canvas.Brush.Style:=bsClear; image1.Canvas.Font.Color :=clBlack; image1.Canvas.TextOut(0, 0, 'hi there'); 

This is text with a transparent background. Or do you want the text itself to be transparent? This is a little trickier. You will need to draw it by hand. An easy way to do this is to sample the average color value of the area you are writing in the image. Then set the font color a little lighter and your shadow will be a little darker. Then it somehow combines.

+3
source share

I have not tested it, but it will give you some idea of ​​where to go. the key is a brush style.

something like that:

 img.Canvas.Brush.Style:=bsClear; img.Canvas.Font.Color:=clBlack; img.Canvas.TextOut(0, 0, 'hi there'); 
+2
source share

This feature is based on the idea of ​​Dave Elsberry.

What else:

  • Draws only the shadow transparently
  • It uses almost 2 times less RAM
  • Options

 procedure DrawShadowText(aCanvas: TCanvas; CONST Text: string; CONST X, Y, Opacity: Integer; TextColor, ShadowColor: TColor); { Opacity a value from 0-255: $00 is completely transparent, $FF is completely opaque. $C0 is 75% opaque } CONST ShadowSize= 1; VAR TempBMP: TBitmap; BlendFunc: BLENDFUNCTION; H, W: Integer; begin BlendFunc.BlendOp := AC_SRC_OVER; BlendFunc.BlendFlags := 0; BlendFunc.SourceConstantAlpha := Opacity; BlendFunc.AlphaFormat := AC_SRC_ALPHA; { Create another TBitmap to hold the text you want to overlay } TempBMP := Graphics.TBitmap.Create; TRY TempBMP.Canvas.Font.Style := [fsBold]; TempBMP.Canvas.Brush.Style := bsClear; W:= TempBMP.Canvas.TextWidth(Text); H:= TempBMP.Canvas.TextHeight(Text); TempBMP.SetSize(W+ShadowSize, H+ShadowSize); { In AlphaBlend, Black is always 100% transparent. So, paint TempBMP completely Black. } TempBMP.Canvas.Brush.Color := clBlack; TempBMP.Canvas.FloodFill(0, 0, clNone, fsBorder); { Write the shadow first } TempBMP.Canvas.Font.Color := ShadowColor; TempBMP.Canvas.TextOut(ShadowSize, ShadowSize, Text); { Diagonal left shadow } TempBMP.Canvas.TextOut(ShadowSize, 0, Text); { Left shadow } { Draw the text with transparency: TempBMP.Canvas.Brush.Style := bsClear; TempBMP.Canvas.Font.Color := TextColor; TempBMP.Canvas.TextOut(0, 0, Text); } { Use the AlphaBlend function to overlay the bitmap holding the text on top of the bitmap holding the original image. } Windows.AlphaBlend(aCanvas.Handle, x, y, TempBMP.Width, TempBMP.Height, TempBMP.Canvas.Handle, 0, 0, TempBMP.Width, TempBMP.Height, BlendFunc); { Draw the text at 100% opacity } aCanvas.Font.Style := [fsBold]; aCanvas.Brush.Style := bsClear; aCanvas.Font.Color := TextColor; aCanvas.TextOut(x, y-1, Text); FINALLY FreeAndNil(TempBMP); END; end; procedure TfrmTest.UseIt; VAR BackgroundImage: tbitmap; begin BackgroundImage := Graphics.TBitmap.Create; try BackgroundImage.LoadFromFile('c:\test.bmp'); DrawShadowText (BackgroundImage.Canvas, 'This is some demo text', 20, 40, 140, clRed, clSilver); Image1.Picture.Bitmap.Assign(BackgroundImage); FINALLY BackgroundImage.Free; end; end; 
+2
source share

You can use bitblt to merge the image with a common canvas, and then save the image again.

+1
source share

All Articles