![]() |
Multimedia »
General Graphics »
Image Display
Intermediate
License: The Code Project Open License (CPOL)
Free Image TransformationBy YLS CSIntroducing a method to transform an image freely with C# |
C#, Windows, GDI+
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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!
Config and Save functions | You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 10 May 2009 Editor: Deeksha Shenoy |
Copyright 2009 by YLS CS Everything else Copyright © CodeProject, 1999-2009 Web20 | Advertise on the Code Project |