delphi - 在图像上写入透明文本

标签 delphi image-processing

如何在图像(Jpg、Bmp)上写入半透明文本,或透明文本(颜色与背景图像相同)但带有阴影,这是我想要为图像添加水印的操作。

我想使用 Delphi win32 来实现这一点。

最佳答案

一种选择是使用 Windows.pas 单元中的 AlphaBlend 函数。像这样的东西会产生覆盖在图像上的半透明文本(带有阴影 - 基于 Jim McKeeth 的响应):

  
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;

关于delphi - 在图像上写入透明文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/360476/

相关文章:

python - 是否有在图像上应用置换贴图/矩阵的功能?

image-processing - 为什么物体检测需要图像分割?

delphi - ARC下数据库连接对象(Mydac TMyConnection)会发生什么

delphi - 内河码头 RAD 工作室 : How to dock component palette

delphi - 多色多线TMemo

Delphi 用标准 Windows GUI 窗口打开文件

delphi - 字符串记录的 TDictionary 哈希被破坏

python - 如何使用PIL将灰度变成假色?

OpenCV StereoSGBM 和 gpu::StereoBM_GPU 之间的巨大差异

python - 使用 opencv Python 删除图像的背景