Click here to Skip to main content
Licence CPOL
First Posted 2 May 2009
Views 27,112
Downloads 883
Bookmarked 52 times

Free Image Transformation

By | 10 May 2009 | Article
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)

About the Author

YLS CS



Unknown

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionNeed anti-aliasing along the edges Pinmemberpeter p.16:04 16 May '12  
GeneralMy vote of 5 PinmemberKevin.zsh20:17 27 Feb '12  
QuestionAs previous post PinmemberTim Grindley4:54 5 Nov '11  
GeneralMy vote of 5 PinmemberTim Grindley4:50 5 Nov '11  
GeneralAwesome PinmemberScrappyPup17:59 23 Sep '10  
Questiontransforming from points PinmemberiLens4:50 8 Jul '10  
GeneralThank this program! PinmemberJiYC4184:54 28 Nov '09  
Generalnot implementing rotation Pinmembermiadmahmud0:40 14 Jun '09  
GeneralExcellent Pinmemberemarti9:30 10 May '09  
Thanks
 
Divinum est sedare dolorem

GeneralGreat solution! Pinmemberdherrmann22:50 5 May '09  
GeneralRe: Great solution! PinmemberYLS CS2:22 6 May '09  
GeneralRe: Great solution! Pinmemberdherrmann20:44 11 May '09  
GeneralRe: Great solution! PinmemberYLS CS4:16 12 May '09  
GeneralRe: Great solution! Pinmemberdherrmann21:04 12 May '09  
GeneralRe: Great solution! PinmemberYLS CS2:01 13 May '09  
GeneralRe: Great solution! Pinmemberdherrmann22:17 10 Jun '09  
GeneralRe: Great solution! PinmemberMember 781806120:02 27 Feb '12  
GeneralIdea PinmemberGraGra_3313:27 2 May '09  
GeneralRe: Idea PinmemberYLS CS2:12 3 May '09  

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.

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