2010. július 25., vasárnap

How to copy a polygon region from one bitmap to another


Problem/Question/Abstract:

I have 3 Bitmaps. I copy BM2 on BM1. Then the problem: Copy a defined polygon from BM3 (or BM2) into BM1. I only want to map the defined polygon into BM1 without destroying any pixels outside the poly (within the rectangle).

Answer:

Here is one way you can try. It defines a polygon shaped clip region for the destination bitmap, then copies the origin bitmap:

{ ... }
var
  pts: array of TPoint;
  rgn: HRgn;
begin
  SetLength(pts, 4);
  pts[0] := Point(0, 0);
  pts[1] := Point(50, 20);
  pts[2] := Point(20, 50);
  pts[3] := pts[0];
  rgn := CreatePolygonRgn(pts[0], 4, Winding);
  SelectClipRgn(bm1.Canvas.Handle, rgn);
  bm1.Canvas.Copyrect(rect(0, 0, bm2.width, bm2.height),
    bm2.canvas, rect(0, 0, bm2.width, bm2.height);
    DeleteObject(rgn);
end;
{ ... }

Nincsenek megjegyzések:

Megjegyzés küldése