Recent

Author Topic: TBitmap and transparency  (Read 5443 times)

Valdas

  • New Member
  • *
  • Posts: 35
TBitmap and transparency
« on: March 14, 2007, 12:28:51 am »
I'm working on kUbuntu 6.10 with Lazarus 0.9.21 (latest svn).

 I want draw dynamically painted TBitmap with transparency, but i get picture without transparency.
 Transparency with loaded bitmaps worked OK.

 What i doing wrong? Or maybe this problem exist only under Linux?

 Code is bellow. On left form side is drawn dynamically painted bitmap, on right side- for comparison is drawn same bitmap, but previously saved and then loaded.
Code: [Select]

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
  private
    FDrobe: TBitmap;
    FUzkrautas: TBitmap;
    procedure FormDestroy(Sender: TObject);
    procedure FormPaint(Sender: TObject);
  public
    constructor Create(AOwner: TComponent); override;
  end;

var
  Form1: TForm1;

implementation

{ TForm1 }

constructor TForm1.Create(AOwner: TComponent);
const
  FAILAS='testas.bmp';
begin
    inherited Create(AOwner);
    //
    FDrobe:=TBitmap.Create;
    FDrobe.Width:=50;
    FDrobe.Height:=50;
    //
    FDrobe.Canvas.Brush.Color:=clYellow;
    FDrobe.Canvas.FillRect(Rect(0, 0, FDrobe.Width, FDrobe.Height));
    //
    FDrobe.Canvas.Brush.Color:=clRed;
    FDrobe.Canvas.FillRect(Rect(0, FDrobe.Height div 2, FDrobe.Width div 2, FDrobe.Height));
    //
    FDrobe.Transparent:=True;
    FDrobe.TransparentColor:=FDrobe.Canvas.Pixels[0, 0];
    //
    FDrobe.SaveToFile(FAILAS);
    //
    if (FileExists(FAILAS)) then
     begin
        FUzkrautas:=TBitmap.Create;
        FUzkrautas.LoadFromFile(FAILAS);
        //
        FUzkrautas.Transparent:=True;
        FUzkrautas.TransparentColor:=FUzkrautas.Canvas.Pixels[0, 0];
     end;
    //
    OnDestroy:=@FormDestroy;
    OnPaint:=@FormPaint;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
    if (Assigned(FUzkrautas)) then FreeAndNil(FUzkrautas);
    if (Assigned(FDrobe)) then FreeAndNil(FDrobe);
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
    if (Assigned(FDrobe)) then Canvas.Draw(10, 10, FDrobe);
    if (Assigned(FUzkrautas)) then Canvas.Draw(70, 10, FUzkrautas);
end;

initialization
  //{$I unit1.lrs}

end.

VictorZhang

  • New Member
  • *
  • Posts: 11
RE: TBitmap and transparency
« Reply #1 on: March 15, 2007, 06:29:43 pm »
It seems that TBitmap.SetTransparentMode not implemented yet...
The TransparentColor don't work!
J.S.Bach is an evidence of God's existence.

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1890
RE: TBitmap and transparency
« Reply #2 on: March 15, 2007, 06:56:14 pm »
memstream := TMemoryStream.create;
  try
    bmp.SaveToStream(memstream);
    memstream.position := 0;
    bmp.LoadFromStream(memstream);
  finally
    memstream.free;
  end;


See: http://wiki.lazarus.freepascal.org/Developing_with_Graphics#Drawing_color_transparent_bitmaps