16,001,870 members
Articles / Multimedia / GDI+

# Free Image Transformation

Rate me:
4.94/5 (39 votes)
10 May 2009CPOL 138.7K   6.9K   80   36
Introducing a method to transform an image freely with C#

## 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:

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

 First PrevNext
 Interpolation doesn't seem to work? i0018-Apr-24 14:38 i00 18-Apr-24 14:38
 Are you still here. Fat and long member28-Dec-21 5:40 Fat and long member 28-Dec-21 5:40
 bug report wingnet1-Dec-13 15:07 wingnet 1-Dec-13 15:07
 Using Parallel.For to be faster WiiMaxx23-Jul-13 5:32 WiiMaxx 23-Jul-13 5:32
 Great Example! Can you also do the reverse transform (from warped image to rectangle)? NGErndt2-Jul-13 6:07 NGErndt 2-Jul-13 6:07
 Re: Great Example! Can you also do the reverse transform (from warped image to rectangle)? NGErndt2-Jul-13 14:01 NGErndt 2-Jul-13 14:01
 Powerful But Need A Faster Way goddgoh14-May-13 21:04 goddgoh 14-May-13 21:04
 Re: Powerful But Need A Faster Way WiiMaxx23-Jul-13 5:39 WiiMaxx 23-Jul-13 5:39
 Can you show me in your project where is smooth applied? nambatre29-Nov-12 16:58 nambatre 29-Nov-12 16:58
 Very Important!! How do I apply Perspective transformation on the image? S Keller12-Nov-12 7:39 S Keller 12-Nov-12 7:39
 My vote of 5 S Keller5-Nov-12 0:12 S Keller 5-Nov-12 0:12
 Must be doing something wrong. Member 88456084-Sep-12 13:29 Member 8845608 4-Sep-12 13:29
 Re: Must be doing something wrong. Member 88456084-Sep-12 14:21 Member 8845608 4-Sep-12 14:21
 My vote of 5 thams15-Aug-12 18:36 thams 15-Aug-12 18:36
 Great job thams15-Aug-12 18:35 thams 15-Aug-12 18:35
 Need anti-aliasing along the edges peter p.16-May-12 16:04 peter p. 16-May-12 16:04
 My vote of 5 Kevin.zsh27-Feb-12 20:17 Kevin.zsh 27-Feb-12 20:17
 As previous post Tim Grindley5-Nov-11 4:54 Tim Grindley 5-Nov-11 4:54
 My vote of 5 Tim Grindley5-Nov-11 4:50 Tim Grindley 5-Nov-11 4:50
 Awesome ScrappyPup23-Sep-10 17:59 ScrappyPup 23-Sep-10 17:59
 transforming from points iLens8-Jul-10 4:50 iLens 8-Jul-10 4:50
 Re: transforming from points love2xlr828-Jan-13 20:43 love2xlr8 28-Jan-13 20:43
 Thank this program! rizherong28-Nov-09 4:54 rizherong 28-Nov-09 4:54
 not implementing rotation miadmahmud14-Jun-09 0:40 miadmahmud 14-Jun-09 0:40
 Excellent emarti10-May-09 9:30 emarti 10-May-09 9:30
 Last Visit: 31-Dec-99 18:00     Last Update: 18-Sep-24 11:32 Refresh 12 Next ᐅ

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

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