当前位置:主页>Delphi教程>文章内容
取得图片的透明区域
来源: 作者: 发布时间:2007-04-29  
(*//
标题:取得图片的透明区域
说明:适用于制作复杂的不规则窗体
设计:Zswang
支持:wjhu111@21cn.com
日期:2004-03-10
//*)
(*//============================================================================
设计思路:~~
就是对画布一行一行的扫描~~
对于不是透明色相连的像素都看成一条条的线段~~
  ───────── ─    ───    ─────
   ───────           ────    ───────
     ───          ─── ──    ───────
                ──      ─────────
用这些线段组合成不规则的区域~~
线段就是找到开始位置和结束位置就行了~~
组合区域是最花时间的地方~~
减少组合区域的频率就可以提高运行的速度~~
用线段组合就比用点组合少多了~~
============================================================================//*)
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;

 
上一篇:GSM规范中的部分编码转换   下一篇:使用IntraWeb进行Web编程
 
  相关文章
·GSM规范中的部分编码转换
·使用IntraWeb进行Web编程
·通用查询组件设计(续二)
·搜索字符串在流中的位置
·在delphi.net的VCL.net里使用Ado.net
·流的压缩和解压
·使用Delphi创建IIS虚拟目录
·剪贴板的流存储
·在WIN2000与WIN98中创建共享文件夹的方
·用Delphi制作DLL小结
·Delphi实现对注册表的监视和扫描
·使用ACTIVEX和DELPHI开发串口通讯
 
【关闭窗口】
推荐本站资源
最新文章