Click here to Skip to main content
14,977,142 members
Articles / Multimedia / GDI+
Posted 2 May 2009

Tagged as


77 bookmarked

Free Image Transformation

Rate me:
Please Sign up or sign in to vote.
4.94/5 (38 votes)
10 May 2009CPOL
Introducing a method to transform an image freely with C#


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:

Image 2

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 = 

Thanks for trying it!


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


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

No Biography provided

Comments and Discussions

Bugbug report Pin
wingnet1-Dec-13 15:07
Memberwingnet1-Dec-13 15:07 
SuggestionUsing Parallel.For to be faster Pin
WiiMaxx23-Jul-13 5:32
MemberWiiMaxx23-Jul-13 5:32 
QuestionGreat Example! Can you also do the reverse transform (from warped image to rectangle)? Pin
NGErndt2-Jul-13 6:07
MemberNGErndt2-Jul-13 6:07 
AnswerRe: Great Example! Can you also do the reverse transform (from warped image to rectangle)? Pin
NGErndt2-Jul-13 14:01
MemberNGErndt2-Jul-13 14:01 
QuestionPowerful But Need A Faster Way Pin
goddgoh14-May-13 21:04
Membergoddgoh14-May-13 21:04 
AnswerRe: Powerful But Need A Faster Way Pin
WiiMaxx23-Jul-13 5:39
MemberWiiMaxx23-Jul-13 5:39 
QuestionCan you show me in your project where is smooth applied? Pin
nambatre29-Nov-12 16:58
Membernambatre29-Nov-12 16:58 
QuestionVery Important!! How do I apply Perspective transformation on the image? Pin
S Keller12-Nov-12 7:39
MemberS Keller12-Nov-12 7:39 
GeneralMy vote of 5 Pin
S Keller5-Nov-12 0:12
MemberS Keller5-Nov-12 0:12 
QuestionMust be doing something wrong. Pin
Member 88456084-Sep-12 13:29
MemberMember 88456084-Sep-12 13:29 
AnswerRe: Must be doing something wrong. Pin
Member 88456084-Sep-12 14:21
MemberMember 88456084-Sep-12 14:21 
GeneralMy vote of 5 Pin
thams15-Aug-12 18:36
Memberthams15-Aug-12 18:36 
GeneralGreat job Pin
thams15-Aug-12 18:35
Memberthams15-Aug-12 18:35 
QuestionNeed anti-aliasing along the edges Pin
peter p.16-May-12 16:04
Memberpeter p.16-May-12 16:04 
GeneralMy vote of 5 Pin
Kevin.zsh27-Feb-12 20:17
MemberKevin.zsh27-Feb-12 20:17 
QuestionAs previous post Pin
Tim Grindley5-Nov-11 4:54
MemberTim Grindley5-Nov-11 4:54 
GeneralMy vote of 5 Pin
Tim Grindley5-Nov-11 4:50
MemberTim Grindley5-Nov-11 4:50 
GeneralAwesome Pin
ScrappyPup23-Sep-10 17:59
MemberScrappyPup23-Sep-10 17:59 
Questiontransforming from points Pin
iLens8-Jul-10 4:50
MemberiLens8-Jul-10 4:50 
AnswerRe: transforming from points Pin
love2xlr828-Jan-13 20:43
Memberlove2xlr828-Jan-13 20:43 
GeneralThank this program! Pin
rizherong28-Nov-09 4:54
Memberrizherong28-Nov-09 4:54 
Generalnot implementing rotation Pin
miadmahmud14-Jun-09 0:40
Membermiadmahmud14-Jun-09 0:40 
GeneralExcellent Pin
emarti10-May-09 9:30
Memberemarti10-May-09 9:30 
GeneralGreat solution! Pin
dherrmann5-May-09 22:50
Memberdherrmann5-May-09 22:50 
GeneralRe: Great solution! Pin
YLS CS6-May-09 2:22
MemberYLS CS6-May-09 2:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.