Author Topic: [Help] a transparent bitmap over another image  (Read 5693 times)

Offline mos

  • Imaging User
  • *
  • Posts: 23
    • View Profile
[Help] a transparent bitmap over another image
« on: 28 August 2008, 20:23:55 »
I want 2 Image (Image1, Image2) of TImage type, one (Image2, foreground) overlaying the other (Image1, background).
The Image1 must contain a Jpeg image, the Image2 must be a transparent (on clBlack color) bitmap over the user can draw lines.

I have write the following code, but this not work, what I obtain is a black image over a background image, but where the white line I draw is transparent!
Instead what I want is a white line over background image.

Can you help me?


Thanks



source code:

unit Unit1;

{$I ImagingOptions.inc}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  StdCtrls,
  ImagingTypes,
  Imaging,
  ImagingClasses,
  ImagingComponents,
  ImagingCanvases,
  ImagingUtility;

type

  { TForm1 }

  TForm1 = class(TForm)
    Image1: TImage;
    Image2: TImage;

  private
    { private declarations }
  public
    { public declarations }
    FBitmap1: TImagingBitmap;
    FImage1: TMultiImage;
   
    FBitmap2: TImagingBitmap;
    FImage2: TMultiImage;

  end;

var
  Form1: TForm1;

implementation

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  FImage1 := TMultiImage.Create;
  FBitmap1 := TImagingBitmap.Create;
  Image1.Picture.Graphic := FBitmap1;
 
  FImage1.LoadMultiFromFile('e:\background.jpg'); // my background image
  Image1.Picture.Graphic.Assign(FImage1);
 
  FImage2 := TMultiImage.Create;
  FBitmap2 := TImagingBitmap.Create;

  Image2.Picture.Graphic := FBitmap2;
  FImage2.LoadMultiFromFile('e:\foreground.bmp'); // my foreground image, a black bitmap of same dimension of background image

  Image2.Picture.Graphic.Assign(FImage2);
  Image2.Picture.Bitmap.Transparent := True;
  Image2.Picture.Bitmap.TransparentColor := clBlack;

  Image2.Canvas.Pen.Color := clWhite;
  Image2.Canvas.Line(0, 0, 200, 200);

end;

initialization
  {$I Unit1.lrs}

end.


« Last Edit: 28 August 2008, 20:26:45 by mos »

Offline Galfar

  • Administrator
  • Imaging User
  • *****
  • Posts: 360
    • View Profile
    • Galfar's Homepage
Re: [Help] a transparent bitmap over another image
« Reply #1 on: 31 August 2008, 00:05:49 »
Did you set Image2.Transparent := True ?
I tried your code and with Image2 set to transparent it worked.

Offline mos

  • Imaging User
  • *
  • Posts: 23
    • View Profile
Re: [Help] a transparent bitmap over another image
« Reply #2 on: 31 August 2008, 09:22:35 »
Really? This is strange. You have try under WinXP?

I have set to True the Transparent property of Image2 in the IDE, and now also in the code but what I see is always a Image1 with the background image and Image2 with a black image and the line that I draw is transparent.

I have attached a screenshot of the window, also I have attached the source code (here naturally you must change the Paths in Compiler Options).


Bye
« Last Edit: 31 August 2008, 09:54:01 by mos »

Offline Galfar

  • Administrator
  • Imaging User
  • *****
  • Posts: 360
    • View Profile
    • Galfar's Homepage
Re: [Help] a transparent bitmap over another image
« Reply #3 on: 31 August 2008, 11:23:22 »
Oh I tried that in Delphi before.

Now I tried your sample in Laz 0.9.24:
If you call this
Code: [Select]
Image2.Canvas.Brush.Color := clBlack;
Image2.Canvas.FillRect(Image2.ClientRect);

before drawing the line, the line won't be transparent but the black
area won't be transparent either.

In Laz 0.9.25 it works ok (but the black FillRect is still needed!) so
I suggest upgrading if you don't use it already.


Offline mos

  • Imaging User
  • *
  • Posts: 23
    • View Profile
Re: [Help] a transparent bitmap over another image
« Reply #4 on: 1 September 2008, 17:12:05 »
I have added your code (on WinXP+SP2 with Lazarus 0.9.24 and on other partition with Lazarus 0.9.25), but I have always a black area with white line:

Code: [Select]
  Image2.Transparent := True;
 
  FImage1 := TMultiImage.Create;
  FBitmap1 := TImagingBitmap.Create;
  Image1.Picture.Graphic := FBitmap1;

  FImage1.LoadMultiFromFile('e:\Tigers.jpg'); // my background image
  Image1.Picture.Graphic.Assign(FImage1);

  FImage2 := TMultiImage.Create;
  FBitmap2 := TImagingBitmap.Create;
  FBitmap2.Width:=449;
  FBitmap2.Height:=303;
  Image2.Picture.Graphic := FBitmap2;
  FImage2.LoadMultiFromFile('e:\black.bmp'); // my foreground image, a black bitmap of same dimension of background image
  Image2.Picture.Graphic.Assign(FImage2);

  Image2.Canvas.Brush.Color := clBlack;
  Image2.Canvas.FillRect(Image2.ClientRect);
  Image2.Canvas.Pen.Color := clWhite;
  Image2.Canvas.Line(0, 0, 200, 200);

Offline Galfar

  • Administrator
  • Imaging User
  • *****
  • Posts: 360
    • View Profile
    • Galfar's Homepage
Re: [Help] a transparent bitmap over another image
« Reply #5 on: 3 September 2008, 13:51:44 »
I copies you last code exactly and this is what I get in 0.9.25:

Offline Galfar

  • Administrator
  • Imaging User
  • *****
  • Posts: 360
    • View Profile
    • Galfar's Homepage
Re: [Help] a transparent bitmap over another image
« Reply #6 on: 3 September 2008, 13:55:08 »
This is what I get in 0.9.24: