0

I would like to Crop an image to a ratio of 5/3.5 when I have the following:

  • Virtual path to image (~/Uploads/Images)
  • X Point for Top left corner of image
  • Y Point for Top left corner of image
  • Image width
  • Image Height

Here is an image showing what I mean:

cropping

The highlighted bits are what I have.

How can I achieve that with C# in my MVC Controller? I'd like to also store the image back to its original location, overwriting the old image if possible.

J86
  • 14,345
  • 47
  • 130
  • 228

1 Answers1

1

You can still use System.Drawing in asp.net even though its not recommended.

from what i understod you need a function with the following signiture

public static void CropAndOverwrite(string imgPath,int x1, int y1, int height, int width)

The task is fairly simple

public static void CropAndOverwrite(string imgPath, int x1, int y1, int height, int width)
    {

        //Create a rectanagle to represent the cropping area
        Rectangle rect = new Rectangle(x1, y1, width, height);
        //see if path if relative, if so set it to the full path
        if (imgPath.StartsWith("~"))
        {
            //Server.MapPath will return the full path
            imgPath = Server.MapPath(imgPath);
        }
        //Load the original image
        Bitmap bMap = new Bitmap(imgPath);
        //The format of the target image which we will use as a parameter to the Save method
        var format = bMap.RawFormat;


        //Draw the cropped part to a new Bitmap
        var croppedImage = bMap.Clone(rect, bMap.PixelFormat);

        //Dispose the original image since we don't need it any more
        bMap.Dispose();

        //Remove the original image because the Save function will throw an exception and won't Overwrite by default
        if (System.IO.File.Exists(imgPath))
            System.IO.File.Delete(imgPath);

        //Save the result in the format of the original image
        croppedImage.Save(imgPath,format);
        //Dispose the result since we saved it
        croppedImage.Dispose();
    }
Xi Sigma
  • 2,292
  • 2
  • 13
  • 16
  • Thanks @decoherence. Why isn't `System.Drawing` recommended? – J86 Mar 25 '15 at 14:59
  • @Ciwan http://stackoverflow.com/questions/390532/system-drawing-in-windows-or-asp-net-services – Xi Sigma Mar 25 '15 at 15:00
  • Thanks, on this line `Image.FromFile(imgPath);` I get an error saying it failed to load the file! `imgPath` is `~/uploads/images`. Do I need to do anything special with the path? – J86 Mar 25 '15 at 15:06
  • @Ciwan use the full path, the path has to point to a single image `c:/uploads/images/someImage.jpg` not sure if `~/uploads/images/someImage.jpg` would work but you can try – Xi Sigma Mar 25 '15 at 15:11
  • Thanks @decoherence. Aye just did some Googling and it turns out `.FromFile()` requires full path, else it'll look in same directory. Any idea how I can get from `~/uploads/images` to `x:/work/app/uploads/images/`? – J86 Mar 25 '15 at 15:14
  • @Ciwan the server machine is not yours? try ResolveUrl("~/uploads/images") – Xi Sigma Mar 25 '15 at 15:15
  • nope, the app will go on shared hosting. I won't own the server. – J86 Mar 25 '15 at 15:17
  • http://stackoverflow.com/questions/7773660/how-to-load-image-with-relative-path-in-bitmap – Xi Sigma Mar 25 '15 at 15:22
  • This `var absoluteImgPath = Server.MapPath(imgPath);` fixed it. – J86 Mar 25 '15 at 15:50
  • @Ciwan sorry i never used asp.net ;) – Xi Sigma Mar 25 '15 at 15:52