2008. január 9., szerda

Easiest Way Drawing Transparent Image


Problem/Question/Abstract:

How to draw an image transparently ?

Answer:

Here is anohter way to draw a transparent image. Only using Delphi properties and method (Image).

Here is the example code:

// make draw proc to draw transparently

procedure MyTransparentDraw(src, dest: TBitmap; x, y: integer; warna: TColor);
begin
  src.Transparent := true;
  src.TransparentMode := tmFixed;
  src.TransparentColor := warna;
  dest.Canvas.Draw(x, y, src);
end;

procedure TForm1.Button1Click(Sender: TObject);
const
  TRANS_COLOR = clYellow; // change with transparent color you want
var
  bmp, bmp2: TBitmap;
begin
  if (OpenPictureDialog1.Execute) then
  begin
    Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
    bmp := TBitmap.Create;
    try
      bmp.Width := Image1.Width;
      bmp.Height := Image1.Height;
      bmp.Assign(Image1.Picture.Bitmap);
      bmp2 := TBitmap.Create;
      try
        bmp2.Width := bmp.Width;
        bmp2.Height := bmp.Height;
        MyTransparentDraw(bmp, bmp2, 0, 0, TRANS_COLOR);
        Image1.Canvas.Draw(0, 0, bmp2);
      finally
        bmp2.Free;
      end;
    finally
      bmp.Free;
    end;
  end;
end;

To try above code, just copy and paste those code, then click on button to choose an image to be drawn transparently. You can change the value of "TRANS_COLOR" above with any other color that you want to be the transparent color of you image.

Nincsenek megjegyzések:

Megjegyzés küldése