Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 04-14-2014, 02:16 PM
Badger Badger is offline
Member
 
Join Date: Aug 2013
Location: Sheffield, Tasmania (Aust)
Posts: 76
Default Transparency of Bitmap

If I use a polygon's brush.bitmap property to produce custom shading then the "white" portions are not transparent.

Is there a way to make the white pixels in the bitmap transparent?

Badger
Reply With Quote
  #2  
Old 04-14-2014, 07:53 PM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 841
Default

Try to set Transparent property to True and TransparentColor to clWhite.
Reply With Quote
  #3  
Old 04-18-2014, 01:47 AM
Badger Badger is offline
Member
 
Join Date: Aug 2013
Location: Sheffield, Tasmania (Aust)
Posts: 76
Default

I tried that but it didn't seem to work.

Wrote some code in a separate project and this still didn't work. The bitmap Shadows50 is an 8x8 bitmap with alternate black and white pixels.

This is the code.

Code:
 
 unit Unit4;
 interface
 uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
 type
  TForm4 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 var
  Form4: TForm4;
 implementation
 {$R *.dfm}
  
 procedure TForm4.Button1Click(Sender: TObject);
 var
   Bitmap : TBitMap;
begin
  form4.Canvas.Brush.Color:=clRed;
  form4.Canvas.Ellipse(100,100,200,200);
  Bitmap := TBitmap.Create;
  try
    with Bitmap do
    begin
      LoadFromFile('Shadows50.bmp');
      TransparentMode:=tmfixed;
      transparentcolor:=clwhite;
      transparent:=true;
      form4.Canvas.Brush.Bitmap:=bitmap;
      form4.Canvas.Ellipse(150,150,250,250);
    end;
  finally
    Bitmap.Free;
  end;
end;
 end.
Shouldn't I be able to see the whole of the red circle including that behind the shaded part.

Badger
Reply With Quote
  #4  
Old 04-18-2014, 08:37 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,333
Default

I would try this:
http://stackoverflow.com/questions/1...cy-and-opacity
Reply With Quote
  #5  
Old 04-18-2014, 09:44 AM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 841
Default

You want to draw a transparent bitmap inside an ellipse, for this purpose you use Canvas.Brush.Bitmap.
It seems like a good and simple idea, but unfortunately you cannot set transparency for Canvas.Brush.Bitmap. So we have
to find a workaround:

1. I created my own Shadows50.bmp. You can use yours but you have to
resize it(make it bigger, at least the size of the ellipse)


2. Here is the code(I create a region from the second ellipse, and draw the transparent bitmap to that region)
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  Rgn: HRGN;
  Dc: HDC;
  Bmp: TBitmap;
begin
  Bmp := TBitmap.Create;
  try
    with Bmp do
    begin
      TransparentColor := clWhite;
      Transparent := True;
      TransparentMode := tmAuto;
      LoadFromFile('Shadows50.bmp');
    end;
    with Form1.Canvas do
    begin
      Brush.Color := clRed;
      Ellipse(100, 100, 200, 200);
      Brush.Style := bsClear;
      Ellipse(150, 150, 250, 250);
    end;

    Dc := Form1.Canvas.Handle;
    BeginPath(Dc);
    Form1.Canvas.Ellipse(150, 150, 250, 250);
    EndPath(Dc);
    Rgn := PathToRegion(dc);
    try
      SelectClipRgn(DC, Rgn);
      Form1.Canvas.Draw(150, 150, Bmp);
      SelectCliprgn(DC, 0);
    finally
      DeleteObject(Rgn);
    end;
  finally
    Bmp.Free
  end;
end;
3. This is how it looks like:


Is this what you are after?
Reply With Quote
  #6  
Old 04-18-2014, 02:05 PM
Badger Badger is offline
Member
 
Join Date: Aug 2013
Location: Sheffield, Tasmania (Aust)
Posts: 76
Default

Thanks kolbasz

It is exactly what I'm after ( although, as the bmp name implies, the main program is a graphical representation of shadows and the shadow50 is 50% shade - there are 5 other different densities. - that's a lot of bitmap drawing for me to do).

Thanks to all

Badger
Reply With Quote
  #7  
Old 04-18-2014, 02:41 PM
Badger Badger is offline
Member
 
Join Date: Aug 2013
Location: Sheffield, Tasmania (Aust)
Posts: 76
Default

Just as a matter of interest - would this method work?

Set up a buffer - draw the shadow on the buffer - pick off the shadow as a bitmap - copy the bitmap onto the form canvas with the transparency set.

Badger
Reply With Quote
  #8  
Old 04-18-2014, 06:27 PM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 841
Default

I would draw everything to a transparent bitmap. After the drawing is done you can save it to file, draw to canvas...etc
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 10:26 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.