Click here to Skip to main content
15,884,629 members
Articles / Programming Languages / C#

C# Universal Control Cropping Class

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
3 Oct 2022CPOL 3.4K   4  
Add Cropping to any control with image or backgroundimage
This post shows a class that could be universal to any control that has some kind of image. With this class, you can add cropping to any control with image or backgroundimage.

In this post, I will show how to add cropping to any control with image or backgroundimage.

I love programming especially in graphics.

I ran into a situation where I needed to crop a simple picturebox and it wasn't as easy as anticipated.

Then I wanted to crop a panel background image and started rewriting the code for that.

I brainstormed about how a class could be universal to any control that has some kind of image.

Voila! This class was born.

CropForm

Note: The whole crop rectangle can be dragged using right mouse down and drag crop rectangle to desired are inside the control.

Use the drag by left mouse down on any of the drag handles, drag the crop rectangle in different directions.

Usage is fairly simple:

  1. Create an instance of the crop class in form:

    C#
    ControlCrop ControlrectPanel;
    
    public frmCrop()
    {
        InitializeComponent();
    
        //Set crop control for each control that requires cropping
    
        //Set the Control in this case panel to add crop rectangle
        ControlrectPanel = new ControlCrop(pnlCrop);
        ControlrectPanel.SetControl(this.pnlCrop);
    
        //Set the Control in this case Picturebox to add crop rectangle
        ControlrectPicturebox = new ControlCrop(picCrop);
        ControlrectPicturebox.SetControl(this.picCrop);
    }
  2. The crop button code:

    C#
    private void btnCrop_Click(object sender, EventArgs e)
    {
        try
        {
            //Process Panel
            Rectangle rectPanel = new Rectangle
            (ControlrectPanel.rect.X, ControlrectPanel.rect.Y, 
             ControlrectPanel.rect.Width, ControlrectPanel.rect.Height);
    
            //set cropped image size and creat new bitmap
            Bitmap _pnlimg = new Bitmap(ControlrectPanel.rect.Width, 
                                        ControlrectPanel.rect.Height);
    
            //Create the original image to be cropped
            Bitmap OriginalPanelImage = 
            new Bitmap(pnlCrop.BackgroundImage, pnlCrop.Width, pnlCrop.Height);
    
            //create graphic with using statement to auto close grahics
            using (Graphics g = Graphics.FromImage(_pnlimg))
            {
                g.InterpolationMode = 
                System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.PixelOffsetMode = 
                System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                g.CompositingQuality = 
                System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                ////set image attributes
                g.DrawImage(OriginalPanelImage, 0, 0, rectPanel, GraphicsUnit.Pixel);
                picCroppedPanel.Image = _pnlimg;
            }
    
            //Process Picturebox
            Rectangle rectPicturebox = new Rectangle
            (ControlrectPicturebox.rect.X, ControlrectPicturebox.rect.Y, 
                                           ControlrectPicturebox.rect.Width,
            ControlrectPicturebox.rect.Height);
            //set cropped image size and creat new bitmap
            Bitmap _pboximg = new Bitmap(ControlrectPicturebox.rect.Width, 
                                         ControlrectPicturebox.rect.Height);
    
            //Create the original image to be cropped
            Bitmap OriginalPictureboxImage = new Bitmap(picCrop.Image, 
                                             picCrop.Width, picCrop.Height);
    
            //Create graphic with using statement to auto close grahics
            using (Graphics gr = Graphics.FromImage(_pboximg))
            {
                gr.InterpolationMode = 
                   System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                gr.PixelOffsetMode = 
                   System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                gr.CompositingQuality = 
                   System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                ////set image attributes
                gr.DrawImage(OriginalPictureboxImage, 0, 0, 
                             rectPicturebox, GraphicsUnit.Pixel);
                picCropped.Image = _pboximg;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
This article was originally posted at https://github.com/DwainSnickles/ControlCrop

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --