12,622,517 members (28,904 online)
alternative version

69.7K views
68 bookmarked
Posted

# Free Image Transformation

, 10 May 2009 CPOL
 Rate this:
Introducing a method to transform an image freely with C#

## Introduction

I have written a small but powerful C# application that can scale, rotate, skew and distort an image. This program includes a user control `Canvas` and a class `FreeTransform`. `Canvas` can keep the picture in the center of window always, and let the user zoom the image by mouse wheel. You can pick up the corner of the picture by mouse left button and move it freely in `Canvas`. Image transformation is done by the class `FreeTransform`. When you set its `Bitmap` and `FourCorners`, you can get the transformed `Bitmap`. If you like high quality of picture, you can set `IsBilinearInterpolation` to `true`. How does it work? The following diagram demonstrates the key to this method:

The shortest distances of point P to image borders are `w1`, `w2`, `h1 `and `h2`. The position of point P on the original image is supposed to be at:

`([w1/(w1+w2)]*imageWidth, [h1/(h1+h2)]*imageHeight) `

Then the colors at...

`([w1/(w1+w2)]*imageWidth, [h1/(h1+h2)]*imageHeight) `

... on the original image were put on the point P and thus the result.
To calculate of the distances, the vector cross product was used:

```dab = Math.Abs((new YLScsDrawing.Geometry.Vector(vertex[0], srcPt)).CrossProduct(AB));
dbc = Math.Abs((new YLScsDrawing.Geometry.Vector(vertex[1], srcPt)).CrossProduct(BC));
dcd = Math.Abs((new YLScsDrawing.Geometry.Vector(vertex[2], srcPt)).CrossProduct(CD));
dda = Math.Abs((new YLScsDrawing.Geometry.Vector(vertex[3], srcPt)).CrossProduct(DA));

ptInPlane.X = (float)(srcW * (dda / (dda + dbc)));ptInPlane.Y =
(float)(srcH*(dab/(dab+dcd)));```

Thanks for trying it!

## History

• 2nd May, 2009: Initial post
• 8th May, 2009: Added `Config` and `Save` functions

## Share

 Unknown
No Biography provided

## You may also be interested in...

 Pro Pro

 First PrevNext
 bug report wingnet1-Dec-13 16:07 wingnet 1-Dec-13 16:07
 Using Parallel.For to be faster WiiMaxx23-Jul-13 6:32 WiiMaxx 23-Jul-13 6:32
 Great Example! Can you also do the reverse transform (from warped image to rectangle)? NGErndt2-Jul-13 7:07 NGErndt 2-Jul-13 7:07
 Re: Great Example! Can you also do the reverse transform (from warped image to rectangle)? NGErndt2-Jul-13 15:01 NGErndt 2-Jul-13 15:01
 Powerful But Need A Faster Way goddgoh14-May-13 22:04 goddgoh 14-May-13 22:04
 Re: Powerful But Need A Faster Way WiiMaxx23-Jul-13 6:39 WiiMaxx 23-Jul-13 6:39
 Can you show me in your project where is smooth applied? nambatre29-Nov-12 17:58 nambatre 29-Nov-12 17:58
 Very Important!! How do I apply Perspective transformation on the image? S Keller12-Nov-12 8:39 S Keller 12-Nov-12 8:39
 My vote of 5 S Keller5-Nov-12 1:12 S Keller 5-Nov-12 1:12
 Must be doing something wrong. Member 88456084-Sep-12 14:29 Member 8845608 4-Sep-12 14:29
 Re: Must be doing something wrong. Member 88456084-Sep-12 15:21 Member 8845608 4-Sep-12 15:21
 My vote of 5 thams15-Aug-12 19:36 thams 15-Aug-12 19:36
 Great job thams15-Aug-12 19:35 thams 15-Aug-12 19:35
 Need anti-aliasing along the edges peter p.16-May-12 17:04 peter p. 16-May-12 17:04
 My vote of 5 Kevin.zsh27-Feb-12 21:17 Kevin.zsh 27-Feb-12 21:17
 As previous post Tim Grindley5-Nov-11 5:54 Tim Grindley 5-Nov-11 5:54
 My vote of 5 Tim Grindley5-Nov-11 5:50 Tim Grindley 5-Nov-11 5:50
 Awesome ScrappyPup23-Sep-10 18:59 ScrappyPup 23-Sep-10 18:59
 transforming from points iLens8-Jul-10 5:50 iLens 8-Jul-10 5:50
 Re: transforming from points love2xlr828-Jan-13 21:43 love2xlr8 28-Jan-13 21:43
 Thank this program! JiYC41828-Nov-09 5:54 JiYC418 28-Nov-09 5:54
 Excellent emarti10-May-09 10:30 emarti 10-May-09 10:30
 Great solution! dherrmann5-May-09 23:50 dherrmann 5-May-09 23:50
 Re: Great solution! YLS CS6-May-09 3:22 YLS CS 6-May-09 3:22
 Last Visit: 31-Dec-99 19:00     Last Update: 3-Dec-16 9:26 Refresh 12 Next »