Delphi obtains the transparent area of the picture

Source: Internet
Author: User
Tags transparent color

(*//

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.