Click here to Skip to main content
6,629,885 members and growing! (23,194 online)
Email Password   helpLost your password?
Multimedia » General Graphics » Image Display     Intermediate License: The Code Project Open License (CPOL)

Free Image Transformation

By YLS CS

Introducing a method to transform an image freely with C#
C#, Windows, GDI+
Version:7 (See All)
Posted:2 May 2009
Updated:10 May 2009
Views:9,554
Bookmarked:36 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
15 votes for this article.
Popularity: 5.50 Rating: 4.68 out of 5

1

2
1 vote, 6.7%
3
3 votes, 20.0%
4
11 votes, 73.3%
5
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


Member

Location: United States United States

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Barcode Image Generation Library
    This library was designed to give an easy class for developers to use when they need to generate barcode images from a string of data.
  • ImageStone
    An article on a library for image manipulation.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 11 of 11 (Total in Forum: 11) (Refresh)FirstPrevNext
Generalnot implementing rotation Pinmembermiadmahmud1:40 14 Jun '09  
GeneralExcellent Pinmemberemarti10:30 10 May '09  
GeneralGreat solution! Pinmemberdherrmann23:50 5 May '09  
GeneralRe: Great solution! PinmemberYLS CS3:22 6 May '09  
GeneralRe: Great solution! Pinmemberdherrmann21:44 11 May '09  
GeneralRe: Great solution! PinmemberYLS CS5:16 12 May '09  
GeneralRe: Great solution! Pinmemberdherrmann22:04 12 May '09  
GeneralRe: Great solution! PinmemberYLS CS3:01 13 May '09  
GeneralRe: Great solution! Pinmemberdherrmann23:17 10 Jun '09  
GeneralIdea PinmemberGraGra_3314:27 2 May '09  
GeneralRe: Idea PinmemberYLS CS3:12 3 May '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin 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