Search code examples
c#jquerycropjcrop

how to crop an image


I am using Jcrop to try crop and image and save it.

http://deepliquid.com/content/Jcrop.html

Out of the demos this is the one i am implementing:

http://deepliquid.com/projects/Jcrop/demos.php?demo=handler

It gives me several coordinates x1, y1, x2, y2, x, y

once these are submited how do i use them to crop the image?

I have looked at http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing

is this the best way? and it is only using x, y, w and h


Solution

  • Using Graphics.DrawImage is a better idea to crop the image in c#

    Rectangle cropRect = new Rectangle(...);
    Bitmap src = Image.FromFile(fileName) as Bitmap;
    Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);
    
     using(Graphics g = Graphics.FromImage(target))
     {
      g.DrawImage(src, new Rectangle(0, 0, target.Width, target.Height), 
                    cropRect,                        
                    GraphicsUnit.Pixel);
     }
    

    From the source Thread