Search code examples
delphilayergraphics32

Draw a transparent Ellipse in a layer


I have a TImgView32(named CityMap) on my form and an image is loaded on it. Now I create a layer(TBitmapLayer) and draw a circle using Canvas.Ellipse of a TBitmap32 variable like the following:

procedure TfrmMain.Button1Click(Sender: TObject);
var
  tmpBmp: TBitmap32;
  tmpBL: TBitmapLayer;  
begin
  tmpBL:= TBitmapLayer.Create(CityMap.Layers);

  tmpBmp:= TBitmap32.Create;

  with tmpBmp do
  begin
    //Clear;
    SetSize(50, 50);
    Canvas.Brush.Color := clYellow;
    Canvas.Brush.Style:= bsSolid;
    Canvas.Pen.Color := clBlue;
    Canvas.Pen.Width := 2;
    Canvas.Ellipse(Rect(0, 0, 50, 50));
  end;  

  with tmpBL do
  begin
    Scaled:=true;
    Bitmap.DrawMode:=dmBlend;
    tmpBL.Bitmap:=(tmpBmp);
    //tmpBmp.DrawTo(tmpBL.Bitmap, 0, 0); This line doesn't work! So using above line instead
  end;

  //...

end;

The result is like this:

Current result

As you see the problem is that annoying black rectangle. How to create a result like this:

desired result


Solution

  • Use dmTransparent draw mode for the DrawMode property of your TBitmap32 image:

    procedure TForm1.Button1Click(Sender: TObject);
    var
      Bitmap: TBitmap32;
      Layer: TBitmapLayer;
    begin
      Layer := TBitmapLayer.Create(CityMap.Layers);
    
      Bitmap := TBitmap32.Create;
      Bitmap.DrawMode := dmTransparent;
      Bitmap.SetSize(50, 50);
      Bitmap.Canvas.Brush.Color := clYellow;
      Bitmap.Canvas.Brush.Style:= bsSolid;
      Bitmap.Canvas.Pen.Color := clBlue;
      Bitmap.Canvas.Pen.Width := 2;
      Bitmap.Canvas.Ellipse(Rect(0, 0, 50, 50));
    
      Layer.Scaled := True;
      Layer.Bitmap := Bitmap;
      ...
    end;