-1

I need to cut part of picture from pictureBox using rectangle which i have in this picturebox. For now i load image to picture box (and zoom it to picturebox) and draw resizable rectangle inside picturebox, but i don't know how to cut for example left down corner of picturebox by this rectangle.

EDIT

Example I need cut wheel by this rectangle and save it to jpeg but this rectangle is not static.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Skylin R
  • 2,111
  • 1
  • 20
  • 23

2 Answers2

2

I'm not entirely sure I understand your full requirement, but you can use BitMap's Clone method to crop an image. For example, this crops an image in one pictureBox1 and loads it into pictureBox2:

using (Bitmap bmp = new Bitmap(pictureBox1.Image))
{
    var newImg = bmp.Clone(
        new Rectangle { X = 10, Y = 10, Width = bmp.Width / 2, Height = bmp.Height / 2 }, 
        bmp.PixelFormat);
    pictureBox2.Image = newImg;
}

It is very easy to leak Handles when manipulating images. You will need to be careful to dispose of pictureBox2.Image later, especially if you're reloading the image multiple times.

Phil
  • 125
  • 1
  • 7
  • Ok, but will it cut that part which I point by my movable rectangle? For example can i draw by mouse rectangle (i have that part) and by this rectangle cut face from photo in picturebox and save it with my size? – Skylin R Nov 08 '15 at 19:36
  • Yes, you can adapt the code fragment to do that. In my example, I crop at coordinates X=10, Y=10, so you would replace these values with the location of your movable rectangle relative to the picture. The next two arguments are width and height, so you'd use the width and height of your inner rectangle. – Phil Nov 09 '15 at 08:29
  • Thanks a lot Phil that's what i wanted. If i can ask last question, how to cut picture 400x400 and save it to image 200x200 or other set by me? Because i resize rectangle, and i cut by it, but i need to save part of image to defined size file. Should i set it in save method or in cropping? – Skylin R Nov 14 '15 at 22:00
  • Resizing can be as simple as using the Bitmap constructor overload: `new Bitmap(Image, Size)` if quality is not important. For more sophisticated resizing, check out [this question](http://stackoverflow.com/questions/1922040/resize-an-image-c-sharp). I would recommend wrapping your resize operation in its own function and apply it before calling Save, just because it is good practice to ensure each function has a single purpose. – Phil Nov 16 '15 at 08:18
0

Asumming you can create your rectangle and deal with it (drawing it on your picture box, calculating its area...):

    private static Image CropImage(Image img, Rectangle cropArea)
    {
        try {
            Bitmap bmpImage = new Bitmap(img);
            Bitmap bmpCrop = bmpImage.Clone(cropArea /*your rectangle area*/, bmpImage.PixelFormat);
            return (Image)(bmpCrop);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "CropImage()");
        }
        return null;
    }

    private void saveJpeg(string path, Bitmap img, long quality)
    {
        EncoderParameter qualityParam = new EncoderParameter(
                System.Drawing.Imaging.Encoder.Quality, (long)quality);

        ImageCodecInfo jpegCodec = getEncoderInfo("image/jpeg");

        if (jpegCodec == null)
        {
            MessageBox.Show("Can't find JPEG encoder?", "saveJpeg()");
            return;
        }
        EncoderParameters encoderParams = new EncoderParameters(1);
        encoderParams.Param[0] = qualityParam;

        img.Save(path, jpegCodec, encoderParams);
    }

    private ImageCodecInfo getEncoderInfo(string mimeType)
    {
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

        for (int i = 0; i < codecs.Length; i++)
            if (codecs[i].MimeType == mimeType)
                return codecs[i];

        return null;
    }

    private void btnPerformSaveImage_Click(object sender, EventArgs e)
    {
        try
        {
            Image img = (Bitmap)CropImage(new Bitmap(pictureBox1.Image, pictureBox1.Size), CropRect);
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "btnOK_Click()");
        }
    }
Nam Bình
  • 412
  • 2
  • 13
  • Ps: Try googling before post for help. Above codes is modded for personal uses from [this example](http://www.codeproject.com/Articles/32098/Simple-Image-Editor-with-Crop-and-Resize-while-Mai). – Nam Bình Nov 09 '15 at 04:18