Click here to Skip to main content
11,631,906 members (59,412 online)
Click here to Skip to main content

Free Image Transformation

, 10 May 2009 CPOL 55.8K 3.3K 64
Rate this:
Please Sign up or sign in to vote.
Introducing a method to transform an image freely with C#
picture.JPG

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

License

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

Share

About the Author

YLS CS
Unknown
No Biography provided

You may also be interested in...

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
JiYC41828-Nov-09 4:54
memberJiYC41828-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    Rant Rant    Admin Admin   

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150728.1 | Last Updated 10 May 2009
Article Copyright 2009 by YLS CS
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid