(*//
Title: Get the transparent area of the picture
Description: Suitable for making complex irregular forms
Design: Zswang
Support: wjhu111@21cn.com
Date: 2004-03-10
//*)
(*//============================================================================
Design idea: ~ ~
is to scan a line of canvas ~ ~
For not transparent color connected pixels are considered as a line segment ~ ~
──────────────────
──────────────────
───────────────
───────────
Combine these segments into irregular areas ~ ~
The line segment is to find the starting position and the end position is OK ~ ~
The combination area is the most time-consuming place ~ ~
Reduce the frequency of the combined area can increase the speed of operation ~ ~
The combination of line segments is much less than the combination of points ~ ~
============================================================================//*)
function Graphictorgn (mgraphic:tgraphic; mtranspoint:tpoint): Hrgn;
Var
I, J:integer;
Vstart:integer;
Vhandle:hrgn;
Vtranscolor:tcolor;
Begin
Result: = 0;
If not assigned (mgraphic) then Exit;
Result: = CreateRectRgn (0, 0, 0, 0);
With Tbitmap.create do try
Width: = Mgraphic.width;
Height: = Mgraphic.height;
Canvas.draw (0, 0, mgraphic);
Vtranscolor: = Canvas.pixels[mtranspoint.x, Mtranspoint.y];
For I: = 0 to Height-1 do begin
Vstart: = 0;
For J: = 0 to Width do begin
if (Canvas.pixels[j, I] <> vtranscolor) and (J < Width) Then
If Vstart < 0 Then
Vstart: = J
Else
else if Vstart >= 0 THEN BEGIN
Vhandle: = CreateRectRgn (Vstart, I, J, i + 1);
Try
Combinergn (result, result, vhandle, rgn_or);
Finally
DeleteObject (Vhandle);
End
Vstart: =-1;
End
End
End
Finally
Free;
End
End {GRAPHICTORGN}
Example
Procedure Tform1.button1click (Sender:tobject);
Var
Vrgn:hrgn;
Begin
BorderStyle: = Bsnone;
Image1.left: = 0;
Image1.top: = 0;
Vrgn: = Graphictorgn (Image1.Picture.Graphic, point (0, 0));
Try
SetWindowRgn (Handle, Vrgn, True);
Finally
DeleteObject (VRGN);
End
End