Recent

Author Topic: TImage with rounded corners  (Read 1110 times)

codeman

  • Jr. Member
  • **
  • Posts: 78
TImage with rounded corners
« on: April 30, 2022, 06:44:24 pm »
Hi ALL

How can i display a picture from file with rounded cornders?
I want to display img1 (see on attachments) like img2 with rounded corners, i cant find the way, any help is appreciated, thanks!

I get rounded image but its empty..

Code: [Select]
     img2.Picture.LoadFromFile('..');
     img2.Picture.Bitmap.SetSize(img2.Width, img2.Height);
     img2.Transparent:= True;
     img2.Picture.Bitmap.TransparentColor:=$FFFEFD;
     img2.Canvas.Brush.Color:=$FFFEFD;
     img2.Canvas.FillRect(0, 0, img2.Width, img2.Height);
     //img2.Canvas.Brush.Color:=clBlack;
     //img2.Canvas.Pen.Color:=clBlack;
     img2.Canvas.RoundRect(0, 0, img2.Width, img2.Height, 50, 50);
« Last Edit: April 30, 2022, 06:45:59 pm by codeman »
Lazarus v2.02 Win10

jamie

  • Hero Member
  • *****
  • Posts: 6131
Re: TImage with rounded corners
« Reply #1 on: April 30, 2022, 07:07:57 pm »
I think what you are looking for is a region applied to the canvas.

First look here
https://lazarus-ccr.sourceforge.io/docs/lcl/lclintf/createroundrectrgn.html

Create the surface area you wish and then apply it to the canvas you are drawing on, then draw the image to it.

The corners will be excluded from the drawing and give you a rounded corner results.

but then again, I maybe off on another planet.
The only true wisdom is knowing you know nothing

paweld

  • Hero Member
  • *****
  • Posts: 1003
Re: TImage with rounded corners
« Reply #2 on: April 30, 2022, 09:27:00 pm »
try BGRABitmap: https://github.com/bgrabitmap/bgrabitmap/releases
tutorials: https://wiki.freepascal.org/BGRABitmap_tutorial
Sample code:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   image, bmp: TBGRABitmap;
  4. begin
  5.   image := TBGRABitmap.Create('d:\bear.jpg');
  6.   bmp := TBGRABitmap.Create(image.Width + 1, image.Height + 1, panel1.Color);
  7.   bmp.FillRoundRectAntialias(0, 0, image.Width, image.Height, 15, 15, image);
  8.   bmp.RoundRectAntialias(1, 1, image.Width - 1, image.Height - 1, 15, 15, clBlack, 2);
  9.   image.Free;
  10.   bmp.Draw(panel1.Canvas, (panel1.Width - bmp.Width) div 2, (panel1.Height - bmp.Height) div 2, True);
  11.   bmp.Free;
  12. end;
Best regards / Pozdrawiam
paweld

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: TImage with rounded corners
« Reply #3 on: May 01, 2022, 04:53:38 pm »
There's a component in BGRAControls, TBCRoundedImage.

 

TinyPortal © 2005-2018