I would like to draw a transparent TMetaFile on a Canvas, used for print watermark.
The problem is AlphaBlend function won't recognize TMetaFile.Handle as source as it expects canvas handle.
I created a 32bit bitmap with transparency but drawing on it will force white background thus on Canvas you can see an alpha blended rectangle. How can I paint a transparent TMetaFile on screen/printer Canvas?
TMetaFile is surely transparent, I have tested it on simple web page using IE with non-standard background color. The source code:
procedure TPainter.DrawAlpha(ACanvas: TCanvas; ARect: TRect; AGraphic: TMetafile; AAlpha: Byte);
var
bmp: TBitmap;
bf: BLENDFUNCTION;
begin
bmp := TBitmap.Create;
try
bmp.PixelFormat := pf32bit;
bmp.SetSize(ARect.Right - ARect.Left, ARect.Bottom - ARect.Top);
bmp.Transparent := True;
bmp.Canvas.Brush.Color := clNone;
bmp.Canvas.Brush.Style := bsClear;
bmp.Canvas.FillRect(Rect(0, 0, ARect.Right - ARect.Left, ARect.Bottom - ARect.Top));
bmp.Canvas.StretchDraw(Rect(0, 0, ARect.Right - ARect.Left, ARect.Bottom - ARect.Top), AGraphic);
bf.BlendOp := AC_SRC_OVER;
bf.BlendFlags := 0;
bf.SourceConstantAlpha := 230;
bf.AlphaFormat := 0;
AlphaBlend(
ACanvas.Handle,
ARect.Left,
ARect.Top,
ARect.Right - ARect.Left,
ARect.Bottom - ARect.Top,
bmp.Canvas.Handle,
0,
0,
ARect.Right - ARect.Left,
ARect.Bottom - ARect.Top,
bf
);
finally
bmp.Free;
end;
end;