2008-12-11 28 views
6

Resimde yarı saydam bir metin (Jpg, Bmp) veya saydam bir metin (aynı arka plan görüntüsü olarak renkli) nasıl yazabilirim, ancak gölgeli, görüntüleri filigranlamak için yapmak istediğim bir şeyle .Resimde Şeffaf Metin Yazma

Bunu Delphi win32 kullanarak yapmak istiyorum.

cevap

3

İçinde gerçekleştirmeye çalıştığınız şeyin, şeffaf bir arka plan ile yazı yazmaktan biraz daha karmaşık olduğunu varsayarım; Yani, görüntü üzerine yazılmış bir tür alfa harmanlanmış metin elde etmeye çalışıyorsunuz.
En basit yöntem GDI + rutinlerini kullanmaktır. Bunlar delphi için kapsüllenmiş ve http://www.progdigy.com/'dan indirilebilir. Örnek olarak kullanılabilir olması gereken birçok örnek var.

+0

Progdigy.com çalışmıyor mu? Erişim yapılamıyor. – Ampere

+0

Delphi ve Embarcadero için GDI plus' geliştiricisi arasında bir anlaşmazlık varmış gibi görünüyor, buna yanıt olarak çekilip çekilmediğini bilmiyorum. Erik Van Bilsen'in [GDI plus] (http://www.bilsen.com/gdiplus/index.shtml) gibi alternatifleri aramayı deneyebilirsiniz. – Petesh

2

Bunu test etmedim, ancak nereye gideceğiniz konusunda size biraz fikir verecektir. anahtar fırça tarzıdır. Böyle

şey:

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

Röntgen, işte yazacak kullanır çizer siyah renkli metin, metnin yarı transpare olmasını istiyorum nt veya saydam gölgeli –

+0

Yazı tipi rengi kullanılır, değil kalem rengi –

+0

> Yazı tipi rengi kullanılır, değil kalem rengi olarak kullanılır Düzeltme için teşekkürler, jim. –

3

gölge kolaydır:

// 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'); 

Bu şeffaf arka plan ile metindir. Ya da metnin kendisinin simi şeffaf olmasını mı istiyorsunuz? Bu biraz daha zor. Elle çizmeniz gerekecek. Bunun yerine yapmanın kolay bir yolu, görüntü üzerinde yazdığınız alanın renginin ortalamasını örneklemek olacaktır. Ardından yazı tipinizin rengini biraz daha açık olarak ayarlayın ve gölgeniz biraz daha koyu olur. Ardından, bu tür karışımlar,

+0

Jim, ben saydam arka plan ile şeffaf metin istedim, ya da yarı saydam bir metin şeffaf değil arka plan –

+0

Evet emin değilim. Alfa karışımı biraz daha zor. –

+0

Güzel kod, basit ama iş yapıyor. +1 –

1

Bir resmi ortak bir tuvale birleştirmek için bitblt rutinlerini kullanabilir ve ardından görüntüyü tekrar kaydedebilirsiniz.

+1

skamradt, Herhangi bir örnek kod? –

6

Bir seçenek Windows.pas birimindeki AlphaBlend işlevini kullanmaktır. Bu işlev Dave ELSBERRY en fikrine dayanmaktadır


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; 
2

: Bir resmin üzerine yatırılan - Böyle bir şey (Jim McKeeth tepkisi üzerine bina gölge düşen) yarı saydam metin üretecek. Farklı Ne

:

  • şeffaf sadece gölge
  • Neredeyse 2 kat daha az RAM
  • Parametreler

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;