Search code examples
c#asp.netimage-manipulationstretching

Squeezing and stretching of an image on certain coordinates only using C#


I need to stretch and squeeze an image on certain proportion as in the following image is it possible to do this using asp.net C# or is there any Image tools for this.

First one is the original image and second and third are the output images


Solution

  • Found a solution for this using the imagemagick library, it can be downloaded from here.

    Once the reference to the Magick.net dll has been added you can use the following code to distort the image,

    double[] Points =  new double[] {
                    0,0,                0,0,
                    0,500,              50,500,
                    0,1000,             0,1000,
                    420,0,              420,0,
                    420,500,            370,500,
                    420,1000,           420,1000
                };
                MagickImage image = new MagickImage("E:/ImageManipulation/WebApplication2/images/Test.jpg");
                image.Distort(DistortMethod.Shepards,Points);
                image.Write("E:/ImageManipulation/WebApplication2/images/Result.jpg");
    

    See here for some more examples of the Shepards method