Click here to Skip to main content
15,881,882 members
Articles / Multimedia / GDI+

Free Image Transformation

Rate me:
Please Sign up or sign in to vote.
4.94/5 (39 votes)
10 May 2009CPOL 133.9K   6.9K   80   36
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:

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:

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

Then the colors at...

C#
([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:

C#
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)


Written By
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalnot implementing rotation Pin
miadmahmud14-Jun-09 0:40
miadmahmud14-Jun-09 0:40 
GeneralExcellent Pin
emarti10-May-09 9:30
emarti10-May-09 9:30 
GeneralGreat solution! Pin
dherrmann5-May-09 22:50
dherrmann5-May-09 22:50 
GeneralRe: Great solution! Pin
YLS CS6-May-09 2:22
YLS CS6-May-09 2:22 
GeneralRe: Great solution! Pin
dherrmann11-May-09 20:44
dherrmann11-May-09 20:44 
GeneralRe: Great solution! Pin
YLS CS12-May-09 4:16
YLS CS12-May-09 4:16 
GeneralRe: Great solution! Pin
dherrmann12-May-09 21:04
dherrmann12-May-09 21:04 
GeneralRe: Great solution! Pin
YLS CS13-May-09 2:01
YLS CS13-May-09 2:01 
Sorry, I have no idea about VB.

Thanks.
GeneralRe: Great solution! Pin
dherrmann10-Jun-09 22:17
dherrmann10-Jun-09 22:17 
GeneralRe: Great solution! Pin
et327-Feb-12 20:02
et327-Feb-12 20:02 
GeneralIdea Pin
Graeme_Grant2-May-09 13:27
mvaGraeme_Grant2-May-09 13:27 
GeneralRe: Idea Pin
YLS CS3-May-09 2:12
YLS CS3-May-09 2:12 
GeneralRe: Idea Pin
love2xlr828-Jan-13 20:50
love2xlr828-Jan-13 20:50 

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.