Tuesday, April 5, 2011

How do you copy from a rectangular source region to a non-rectangular/non-paralellogram destination region in .NET?

I know how to use the DrawImage() method of the Graphics object to copy from a rectangular source region to a rectangular destination region, and how to copy to a paralellogram region defined by a three-element Point[] array.

Is there any way in .NET to copy from a rectangular source region to a 4-sided non-rectangular destination region (which is defined by four arbitrary points)?

Update. Here is some sample code that copies an image to a parallelogram region:

using (Graphic g = this.CreateGraphics())
{
    List<Point> pts = new List<Point>();
    pts.Add(new Point(0, 0));
    pts.Add(new Point(100, 0));
    pts.Add(new Point(10, 100));
    g.DrawImage(pbSource.Image, pts.ToArray());
}

If I add a fourth point, I get a "Not Implemented" exception.

Update 2. It is possible to do this entirely in .NET:

alt text

but you have to do it yourself, pixel-by-pixel. Graphics courtesy of Jonbert.

From stackoverflow
  • As far as I can tell, there isn't a straightforward (built-in) way to do this kind of image transform in .NET.

    If you look at Anti Aliased Image Transformation (Aaform) over on codeproject, you can download his sample in which there is a pure vb implementation of the concept (as well as a vb and c++ sample).

    There is also Anti-Grain Geometry, a free-to-use (for non-commercial) c++ graphics library and a c# wrapper for it.

    Hope this helps.

  • It is possible to do this entirely in .NET:

    alt text

    but you have to do it yourself, pixel-by-pixel. Graphics courtesy of Jonbert.

0 comments:

Post a Comment