简体   繁体   中英

How to cut picture from pictureBox by rectangle on it

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.

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.

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()");
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM