Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get bitmap transparancy without the need to paint first?

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.

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

  • 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.

Using: Delphi 7, Win 7/64.

like image 942
NGLN Avatar asked Oct 11 '11 20:10

NGLN


1 Answers

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

like image 124
Torbins Avatar answered Sep 28 '22 16:09

Torbins