#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0

    TBitmap TransparentColor with pf1bit problem


    I'm using Delphi 7. Setting the TransparentColor with a pf1bit TBitmap doesn't seem to work. It works with all other pixel formats. Here's an example. I create a pf1bit bitmap with a 2 colour palette. The bitmap is all green apart from a red pixel bottom left. This is drawn without transparency to the form (top). It's then drawn with transparency, setting the TransparentColor to the bottom left pixel - red (bottom). I expect to see the green and no red pixel, but I see the opposite, as if the TransparentColor is set to green. Setting the pixel format to any other format results in the expected result. So it appears that pf1bit behaves differently. Is this a known problem? Is there a solution?

    Code:
    var
      Form1: TForm1;
      bmp : TBitmap;
    
    implementation
    
    procedure TForm1.FormCreate(Sender: TObject);
    Var
      LogicalPalette : TMaxLogPalette;
    begin
      bmp := TBitmap.Create;
      bmp.PixelFormat := pf1bit; // Change to see expected result
      bmp.Width := 300;
      bmp.Height := 200;
    
      LogicalPalette.palVersion := $0300;
      LogicalPalette.palNumEntries := 2;
      LogicalPalette.palPalEntry[0].peBlue  := 0;
      LogicalPalette.palPalEntry[0].peGreen := 255;
      LogicalPalette.palPalEntry[0].peRed   := 0;
      LogicalPalette.palPalEntry[1].peBlue  := 0;
      LogicalPalette.palPalEntry[1].peGreen := 0;
      LogicalPalette.palPalEntry[1].peRed   := 255;
    
      bmp.Palette := CreatePalette(pLogPalette(@LogicalPalette)^);
    
      bmp.Canvas.Brush.Color := $0000FF00;
      bmp.Canvas.FillRect(Rect(0,0,300,200));
      bmp.Canvas.Pixels[0,199] := $000000FF;
    end;
    
    procedure TForm1.FormPaint(Sender: TObject);
    begin
      bmp.Transparent := False;
      Canvas.Draw(20,20,bmp);
    
      bmp.TransparentMode := tmFixed;
      bmp.TransparentColor := bmp.Canvas.Pixels[0,199];
      bmp.Transparent := True;
      Canvas.Draw(20,230,bmp);
    end;
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    7
    Rep Power
    0
    I've now found a workaround for this problem. It seems that there must be an error in Delphi 7 regarding monochrome bitmaps with transparency - possibly when the mask is created.
    Instead of using Canvas.Draw I now use the Windows GDI TransparentBlt directly and everything works fine.

    Code:
    procedure TForm1.FormPaint(Sender: TObject);
    begin
      bmp.Transparent := False;
      Canvas.Draw(20,20,bmp);
    
      {bmp.TransparentMode := tmFixed;
      bmp.TransparentColor := bmp.Canvas.Pixels[0,199];
      bmp.Transparent := True;
      Canvas.Draw(20,230,bmp)}
    
      TransparentBlt(Canvas.Handle, 20, 230, bmp.Width, bmp.Height,
                     bmp.Canvas.Handle, 0, 0, bmp.Width, bmp.Height,
                     bmp.Canvas.Pixels[0,199]);
    end;

IMN logo majestic logo threadwatch logo seochat tools logo