如何在不需要先绘画的情况下获得位图透明度? [英] How to get bitmap transparancy without the need to paint first?

查看:25
本文介绍了如何在不需要先绘画的情况下获得位图透明度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

A newly created bitmap seems to have a (white) background by default. At least, a query on the Pixels property confirms. But why is that background color not used as the transparent color when Transparent is set true?

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

Consider this simple test code:

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;

出于某种原因,整个位图表面必须先绘制才能显示为透明,这听起来有点奇怪.

For some reason, the entire bitmap surface has to be painted before it can appear transparent, which sounds kind of odd.

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

The following variations are tried to eliminate the call to FillRect, all with the same outcome (no transparancy):

  • 只设置Brush.Color,
  • Brush.Handle := CreateSolidBrush(clWhite),
  • PixelFormat := pf32Bit,
  • IgnorePalette := True,
  • TransparantColor := clWhite,
  • TransparantMode := tmFixed,
  • Canvas.Pixels[0, 99] := clWhite 只让那个像素透明,
  • 修改:= True.
  • Only setting Brush.Color,
  • Brush.Handle := CreateSolidBrush(clWhite),
  • PixelFormat := pf32Bit,
  • IgnorePalette := True,
  • TransparantColor := clWhite,
  • TransparantMode := tmFixed,
  • Canvas.Pixels[0, 99] := clWhite which makes only thát pixel transparent,
  • Modified := True.

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

So, the wish is to paint only a portion of a newly created bitmap and get the remaining surface transparent.

使用:Delphi 7,Win 7/64.

Using: Delphi 7, Win 7/64.

推荐答案

在设置位图的尺寸之前只需设置 TransparentColor 和 Canvas.Brush.Color.

Just set TransparentColor and Canvas.Brush.Color before setting dimensions of the bitmap.

这篇关于如何在不需要先绘画的情况下获得位图透明度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆