Hi guys

Hi guys,

This might be a very stupid question but I am unable to make a rounded image in Delphi like the profile pic here. I get my image from a webcam and save it to a database, but I want to make a transparent round png file of the profile image. Would someone be able to assist me please? This is what I have but I can't centre the image before it is made to a circle.

procedure TForm1.Button1Click(Sender: TObject);
var
bmp : TBitmap;
frgn : HRGN;
begin
bmp := TBitmap.Create;
fRgn := CreateEllipticRgn (10, 10, 200, 200);
try
bmp.LoadFromFile('d:\devtest.bmp');
Canvas.Brush.Bitmap := bmp;
PaintRgn(Canvas.Handle,frgn);
FrameRgn(Canvas.Handle,frgn,Canvas.Brush.Handle,2,2);
finally
// Canvas.Brush.Bitmap := null;
bmp.Free;
end;
end;

Comments

  1. You need to select the created region as clip region into your destination canvas, using Windows API function SelectClipRgn(). Then paint your picture into the destination canvas.

    Try this one:

    procedure TForm1.Button1Click(Sender: TObject);
    var
    hRegion : HRGN;
    PNG : TPngImage;
    begin
    Image2.Transparent := True;
    hRegion := CreateEllipticRgn(10, 10, 110, 110);
    SelectClipRgn(Image2.Canvas.Handle, hRegion);
    Image2.Canvas.Draw(0, 0, Image1.Picture.Bitmap);
    SelectClipRgn(Image2.Canvas.Handle, 0);
    DeleteObject(hRegion);

    Image2.Picture.Bitmap.TransparentColor := clWhite;
    Image2.Picture.Bitmap.TransparentMode := tmFixed;

    PNG := TPngImage.Create;
    try
    PNG.Assign(Image2.Picture.Bitmap);
    PNG.SaveToFile('D:\temp\1.png');
    finally
    PNG.Free;
    end;
    end;

    Image1 is a TImage component containing your original picture; Image2 is a TImage component receiving you rounded picture.

    ReplyDelete
  2. Hi Achim, thank you for the reply. I have tested the code but it only saves a blank white png file of 1KB. I copied and pasted code. Is there something else I am doing wrong?

    ReplyDelete

Post a Comment