JonnyKnottsvill
JonnyKnottsvill

Reputation: 1153

Cropping an image from file and resaving in C#

Never really worked with Graphics before. I have looked around on this and pieced together a few solutions from answers which address small parts of my question. but none have worked.

I want to load an image from a file, which will always be 320x240 in size. I then want to crop it to obtain a 240x240 image, with the outer 40px on each side trimmed. After this is done I want to save as a new image.

    private void croptoSquare(string date)
    {
        //Location of 320x240 image
        string fileName = Server.MapPath("~/Content/images/" + date + "contactimage.jpg");

        //New rectangle of final size (I think maybe Point is where I would eventually specify where the crop square site i.e. (40, 0))
        Rectangle cropRect = new Rectangle(new Point(0, 0), new Size(240, 240));
        //Create a Bitmap with correct height/width.
        Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);

        //Load image from file
        using (Image image = Image.FromFile(fileName))
        {
            //Create Graphics object from image
            using (Graphics graphic = Graphics.FromImage(image))
            {
                //Not sure what this does, I found it on a post.
                graphic.DrawImage(image, 
                    cropRect, 
                    new Rectangle(0, 0, target.Width, target.Height),
                    GraphicsUnit.Pixel);

                fileName = Server.MapPath("~/Content/images/" + date + "contactimagecropped.jpg");
                image.Save(fileName);
            }

        }
    }

Currently it is simply resaving the same image and I'm not sure why. I have specified a destination rectangle as 240x240 and a src rectangle as 320x240.

As I say I know basically nothing about working with graphics objects so I imagine this is blatant.

Can anybody tell me how to achieve what I want?

Upvotes: 1

Views: 5866

Answers (2)

Chaz
Chaz

Reputation: 339

private void croptoSquare(string date)
{
    //Location of 320x240 image
    string fileName = Server.MapPath("~/Content/images/" + date + "contactimage.jpg");

    // Create a new image at the cropped size
    Bitmap cropped = new Bitmap(240,240);

    //Load image from file
    using (Image image = Image.FromFile(fileName))
    {
        // Create a Graphics object to do the drawing, *with the new bitmap as the target*
        using (Graphics g = Graphics.FromImage(cropped) )
        {
            // Draw the desired area of the original into the graphics object
            g.DrawImage(image, new Rectangle(0, 0, 240, 240), new Rectangle(40, 0, 240, 240), GraphicsUnit.Pixel);
            fileName = Server.MapPath("~/Content/images/" + date + "contactimagecropped.jpg");
            // Save the result
            cropped.Save(fileName);  
        }
     }

}

Upvotes: 6

Gnqz
Gnqz

Reputation: 3382

Why don't you use JCrop instead? http://www.programmerclubhouse.com/index.php/crop-image-using-jcrop-in-asp-net-c-shar/

Upvotes: 1

Related Questions