如何在不需要先绘制的情况下获得位图透明度?

NGL*_*GLN 10 delphi transparency bitmap

默认情况下,新创建的位图似乎具有(白色)背景.至少,对Pixels属性的查询确认.但是,当Transparent设置为true 时,为什么背景颜色不用作透明颜色?

考虑这个简单的测试代码:

procedure TForm1.Button1Click(Sender: TObject);
var
  Bmp: TBitmap;
begin
  Bmp := TBitmap.Create;
  try
    Bmp.Width := 100;
    Bmp.Height := 100;
    Bmp.Transparent := True;
    Canvas.Draw(0, 0, Bmp);                              // A white block is drawn
    Bmp.Canvas.Brush.Color := Bmp.Canvas.Pixels[0, 99];  // = 'clWhite'
    Bmp.Canvas.FillRect(Rect(0, 0, 100, 100));
    Canvas.Draw(0, 100, Bmp);                            // "Nothing" is drawn
  finally
    Bmp.Free;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

出于某种原因,整个位图表面必须先涂上它才能显得透明,这听起来很奇怪.

尝试以下变体来消除对FillRect所有具有相同结果(没有透明度)的调用:

  • 只有设定Brush.Color,
  • Brush.Handle := CreateSolidBrush(clWhite),
  • PixelFormat := pf32Bit,
  • IgnorePalette := True,
  • TransparantColor := clWhite,
  • TransparantMode := tmFixed,
  • Canvas.Pixels[0, 99] := clWhite 只使像素透明,
  • Modified := True.

因此,希望仅绘制新创建的位图的一部分并使剩余的表面透明.

使用:Delphi 7,Win 7/64.

Tor*_*ins 9

刚设置 TransparentColor和 在设置位图的尺寸之前使用Canvas.Brush.Color.

  • @NGLN我怀疑你甚至需要分配它.只需编写`Canvas.Brush.Handle;`以便创建它. (4认同)
  • 谁知道为什么会这样?你甚至需要设置画笔颜色吗?仅仅强制刷柄存在就足够了吗? (3认同)