gpt4 book ai didi

delphi - Canvas.TransparentColor 和 Canvas.Draw 与不透明度的组合

转载 作者:行者123 更新时间:2023-12-03 15:25:15 25 4
gpt4 key购买 nike

我想在不透明的 Canvas 上绘制位图,其中位图具有透明颜色。

  • 我可以创建一个具有透明颜色的位图并将其绘制到
  • Canvas 我可以创建一个位图并将其绘制到具有不透明度的 Canvas

但我无法将它结合起来。如果我将其组合,则不透明度将被忽略。

这是我编写的代码:

procedure TForm1.FormPaint(Sender: TObject);
var b1,b2:TBitmap;
begin
// Example how it opacity works:
b1 := TBitmap.Create;
b1.SetSize(20,20);
b1.Canvas.Brush.Color := clBlue;
b1.Canvas.Rectangle(0,0,20,20);
Canvas.Draw(10,10,b1,$ff); // Works
Canvas.Draw(40,10,b1,$66); // Works

// I need it in combination with TransparentColor:
b2 := TBitmap.Create;
// next 3 lines are different from above
b2.Transparent := true;
b2.TransparentColor := clFuchsia;
b2.Canvas.Brush.Color := clFuchsia;
b2.SetSize(20,20);
b2.Canvas.Brush.Color := clBlue;
b2.Canvas.Ellipse(0,0,20,20);
Canvas.Draw(10,40,b2,$ff); // Works (full opacity)
Canvas.Draw(40,40,b2,$66); // Ignores the $66 Opacity

b1.Free;
b2.Free;
end;

产生:
enter image description here

我如何绘制具有透明背景且不透明度仅为 40% 的(例如蓝色圆圈)?

如果可能的话,我更喜欢没有直接 winapi 的解决方案(如 bitblt,...)。

我尝试了一些技巧,例如将 alpha channel 位移为 TColor 值,但没有成功。

这是我尝试过的:

procedure TForm1.FormPaint(Sender: TObject);
var b:TBitmap;
begin
b := TBitmap.Create;
b.PixelFormat := pf32bit;
b.AlphaFormat := afDefined;

b.Canvas.Brush.Color := 0 and ($ff shl 32); // Background Transperency
b.SetSize(20,20);
b.Canvas.Brush.Color := clBlue + (($ff-$66) shl 32);
b.Canvas.Ellipse(0,0,20,20);
Canvas.Draw(10,10,b);

b.Free;
end;

产生:
enter image description here

提前致谢!

编辑:我的系统:Windows 7 64位上的delphi xe 5(但使用32位编译器)

最佳答案

发生的情况可以在 procedure TBitmap.DrawTransparent 中看到在图形单元中。
如果图像的属性设置为透明,如示例中 b2 所示,则将使用以下命令绘制位图 Graphics.TransparentStretchBlt正在使用 StretchBlt使用不同的蒙版来绘制图像并且无法使用 Alpha channel 。一个不透明的 Bitmap ,你的 b1,将被绘制 AlphaBlend .

要达到您的目标,您可以使用另一个位图 b2,将 Alphachannel 设置为 0,在 b3 上以不透明度 $66 绘制 b2,将 b2 中 clFuchsia 的每个像素的 Alphachannel 设置为 255,然后使用所需的值绘制此位图不透明度

enter image description here enter image description here

type
pRGBQuadArray = ^TRGBQuadArray;
TRGBQuadArray = ARRAY [0 .. 0] OF TRGBQuad;
TRefChanel=(rcBlue,rcRed,rcGreen);

procedure SetBitmapAlpha(ABitmap: TBitMap; Alpha: Byte);
var
pscanLine32: pRGBQuadArray;
nScanLineCount, nPixelCount : Integer;
begin
with ABitmap do
begin
PixelFormat := pf32Bit;
HandleType := bmDIB;
ignorepalette := true;
alphaformat := afDefined;
for nScanLineCount := 0 to Height - 1 do
begin
pscanLine32 := Scanline[nScanLineCount];
for nPixelCount := 0 to Width - 1 do
with pscanLine32[nPixelCount] do
begin
rgbReserved := Alpha;
end;
end;
end;
end;

procedure AdaptBitmapAlpha(ABitmap,TranspBitmap:TBitmap);
var
pscanLine32,pscanLine32_2: pRGBQuadArray;
nScanLineCount, nPixelCount : Integer;
begin
with ABitmap do
begin
PixelFormat := pf32Bit;
HandleType := bmDIB;
ignorepalette := true;
alphaformat := afDefined;
for nScanLineCount := 0 to Height - 1 do
begin
pscanLine32 := Scanline[nScanLineCount];
pscanLine32_2 := TranspBitmap.Scanline[nScanLineCount];
for nPixelCount := 0 to Width - 1 do
with pscanLine32[nPixelCount] do
begin
// all picels with are not clFuchsia in the transparent bitmap
if NOT ((pscanLine32_2[nPixelCount].rgbBlue=255) AND (pscanLine32_2[nPixelCount].rgbRed=255) AND (pscanLine32_2[nPixelCount].rgbGreen=0) ) then
begin
rgbReserved := 255;
end
else
begin
rgbBlue := 0;
rgbRed := 0;
rgbGreen := 0;
end;
end;
end;
end;
end;



procedure TAForm.FormPaint(Sender: TObject);

var b1,b2,b3:TBitmap;
BF: TBlendFunction;
begin
// Example how it opacity works:
b1 := TBitmap.Create;
b1.SetSize(20,20);
b1.Canvas.Brush.Color := clBlue;
b1.Canvas.Rectangle(0,0,20,20);
Canvas.Draw(10,10,b1,$ff); // Works
Canvas.Draw(40,10,b1,$66); // Works

// I need it in combination with TransparentColor:
b3 := TBitmap.Create;
b3.PixelFormat := pf32Bit;

b2 := TBitmap.Create;
b2.PixelFormat := pf32Bit;
// next 3 lines are different from above
b2.Transparent := true;
b2.TransparentColor := clFuchsia;
b2.Canvas.Brush.Color := clFuchsia;
b2.SetSize(20,20);
b2.Canvas.Brush.Color := clBlue;
b2.Canvas.Ellipse(0,0,20,20);

Canvas.Draw(10,40,b2,$ff); // Works (full opacity)

b3.SetSize(20,20);
SetBitmapAlpha(b3,0);
b3.Canvas.Draw(0,0,b2,$66);
AdaptBitmapAlpha(b3,b2);
Canvas.Draw(40,40,b3,$66);

b1.Free;
b2.Free;
b3.Free;
end;

关于delphi - Canvas.TransparentColor 和 Canvas.Draw 与不透明度的组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26735861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com