🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.delphi.graphics
8 messages
8 total messages Started by notradamus Sun, 03 Aug 2008 13:38
Image, rounded corners
#109
Author: notradamus
Date: Sun, 03 Aug 2008 13:38
10 lines
954 bytes
How can I round the corners of a TImage?

eg. I have a JPEG loaded in a TJpegImage, I want to draw this onto a
TImage canvas, and round off the corners. 

-ND



--- posted by geoForum on http://delphi.newswhat.com
Re: Image, rounded corners
#110
Author: Finn Tolderlund
Date: Sun, 03 Aug 2008 14:20
27 lines
1822 bytes
"notradamus" <nostradumbass77@yahoo.com> skrev i meddelelsen 
news:4895aa8b$1@newsgroups.borland.com...
> How can I round the corners of a TImage?
>
> eg. I have a JPEG loaded in a TJpegImage, I want to draw this onto a
> TImage canvas, and round off the corners.

Put the TImage onto a TPanel or some other container which has a window 
handle.
Set the TImage's Align property to alClient.
Then try this:

procedure TForm1.Button1Click(Sender: TObject);
var
  rgn: HRGN;
  dc : HDC;
begin
  Image1.Picture.LoadFromFile('C:\11.jpg');
  rgn := CreateRoundRectRgn(0, 0, Image1.Width, Image1.Height, 50, 50);
  dc := GetDC(Panel1.Handle);
  SetWindowRgn(Panel1.Handle, rgn, true);
  ReleaseDC(Panel1.Handle, dc);
  DeleteObject(rgn);
end;
-- 
Finn Tolderlund
Re: Image, rounded corners
#111
Author: Chris Rolliston
Date: Sun, 03 Aug 2008 15:28
33 lines
1854 bytes
> procedure TForm1.Button1Click(Sender: TObject);
> var
>  rgn: HRGN;
>  dc : HDC;
> begin
>  Image1.Picture.LoadFromFile('C:\11.jpg');
>  rgn := CreateRoundRectRgn(0, 0, Image1.Width, Image1.Height, 50, 50);
>  dc := GetDC(Panel1.Handle);
>  SetWindowRgn(Panel1.Handle, rgn, true);
>  ReleaseDC(Panel1.Handle, dc);
>  DeleteObject(rgn);
> end;

What's with the DC business, given you don't do anything with it?
Also, you shouldn't delete the region because it becomes owned by
Windows (see the API docs for SetWindowRgn).  So, to slightly amend
your code (and assuming the panel with the TImage to be called
panImage)...

procedure SetRoundRectRegion(Control: TWinControl;
  EllipseWidth, EllipseHeight: Integer);
var
  NewRgn: HRGN;
begin
  NewRgn := CreateRoundRectRgn(0, 0, Control.Width, Control.Height,
    EllipseWidth, EllipseHeight);
  SetWindowRgn(Control.Handle, NewRgn, Control.Showing);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  SetRoundRectRegion(panImage, 50, 50)
end;
Re: Image, rounded corners
#113
Author: Finn Tolderlund
Date: Sun, 03 Aug 2008 19:53
19 lines
2107 bytes
"notradamus" <nostradumbass77@yahoo.com> skrev i meddelelsen 
news:489606bd@newsgroups.borland.com...
> Finn, Chris - thanks for posting those solutions.
>
> But a panel won't do - I really need to manipulate the image because I
> need to insert the image into a RichEdit (TRichView) for screen display in
> a TRVPrintPreview and for printing.
>
> The app is a 'newsletter' creator allowing the user to insert formatted
> text and preview/print/save. The image goes into the header area and needs
> to have rounded corners.

Then create a region as you have been shown here and use the SelectClipRgn 
API on the Canvas.Handle where you want to draw the image. It will clip what 
you draw to only show whats inside the region. That way you will get rounded 
corners of what you draw. Reset the clipping region when done.
-- 
Finn Tolderlund
Re: Image, rounded corners
#112
Author: notradamus
Date: Sun, 03 Aug 2008 20:11
15 lines
1331 bytes
Finn, Chris - thanks for posting those solutions. 

But a panel won't do - I really need to manipulate the image because I
need to insert the image into a RichEdit (TRichView) for screen display in
a TRVPrintPreview and for printing. 

The app is a 'newsletter' creator allowing the user to insert formatted
text and preview/print/save. The image goes into the header area and needs
to have rounded corners.

-ND



--- posted by geoForum on http://delphi.newswhat.com
Re: Image, rounded corners
#116
Author: Finn Tolderlund
Date: Sun, 03 Aug 2008 20:46
9 lines
1496 bytes
"notradamus" <nostradumbass77@yahoo.com> skrev i meddelelsen 
news:489614fc@newsgroups.borland.com...
>  // now the image is stored in
>  // i.Picture.Graphic

Which in this particular case is the same i.Picture.Bitmap.
-- 
Finn Tolderlund
Re: Image, rounded corners
#114
Author: notradamus
Date: Sun, 03 Aug 2008 21:09
38 lines
1964 bytes
>Then create a region as you have been shown here and use the SelectClipRgn 
>API on the Canvas.Handle where you want to draw the image. It will clip what 
>you draw to only show whats inside the region. That way you will get rounded 
>corners of what you draw. Reset the clipping region when done.
>-- 
>Finn Tolderlund
>

I am most grateful, thank you. 

For the benefit of others searching for a similar solution, here is my code:

var
  rgn : HRGN;
begin

  // load image from file
  p := TJpegImage.Create;
  p.LoadFromFile(IMAGEPATH);

  rgn := CreateRoundRectRgn(0, 0, p.Width, p.Height, 50,50);

  i := TImage.Create(self);
  i.SetBounds(0,0,p.Width, p.Height);
  SelectClipRgn( i.Canvas.Handle , rgn );

  i.Canvas.Draw(0,0, p);

  // now the image is stored in
  // i.Canvas.Graphic



-ND



--- posted by geoForum on http://delphi.newswhat.com
Re: Image, rounded corners
#115
Author: notradamus
Date: Sun, 03 Aug 2008 21:12
17 lines
1281 bytes
Correction to the last code:

>  // now the image is stored in
>  // i.Canvas.Graphic

should be:

  // now the image is stored in
  // i.Picture.Graphic


Regards,
ND



--- posted by geoForum on http://delphi.newswhat.com
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads