1

I want to crop an image in c#. As in most photo editing software I want to use the rectangle box which can be resized and repositioned via a mouse. In addition, I would like to know how to highlight the cropped area, as show in this photo.

Jason Down
  • 21,731
  • 12
  • 83
  • 117
qulzam
  • 315
  • 2
  • 9
  • 20
  • What are you looking for here? The actual way of cropping a part from a larger image? Or a way to show your cropping selection like in the picture you linked? – peSHIr Aug 02 '09 at 20:31
  • i have already have some idea about croping. i want to help about way to show cropping selection like in picture. and i also move the cropping selection. – qulzam Aug 03 '09 at 01:25

3 Answers3

1

Your image link is no longer available.

So assuming that in a panel you have your picturebox with the image to crop.

First you need to create event handlers for mouse actions to be able to draw a rectangular region which you wish to crop :

private void picBox_MouseDown(object sender, MouseEventArgs e)
    {
        Cursor = Cursors.Default;
        if (Makeselection)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    Cursor = Cursors.Cross;
                    cropX = e.X;
                    cropY = e.Y;

                    cropPen = new Pen(Color.Crimson, 1);
                    cropPen.DashStyle = DashStyle.Solid;
                }
                picBox.Refresh();
            }
            catch (Exception ex)
            {
            }
        }
    }

    private void picBox_MouseUp(object sender, MouseEventArgs e)
    {
        if (Makeselection)
        {
            Cursor = Cursors.Default;
        }
    }

    private void picBox_MouseMove(object sender, MouseEventArgs e)
    {
        Cursor = Cursors.Default;
        if (Makeselection)
        {
            picBox.Cursor = Cursors.Cross;
            try
            {
                if (picBox.Image == null)
                    return;


                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    picBox.Refresh();
                    cropWidth = e.X - cropX;
                    cropHeight = e.Y - cropY;
                    picBox.CreateGraphics().DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight);
                }
            }
            catch (Exception ex)
            {
            }
        }
    }

    private void picBox_MouseLeave(object sender, EventArgs e)
    {
        tabControl.Focus();
    }

    private void picBox_MouseEnter(object sender, EventArgs e)
    {
        picBox.Focus();
    }

Now, comes the button click function for cropping the image :

 private void btnCrop_Click_1(object sender, EventArgs e)
    {
        Cursor = Cursors.Default;

        try
        {
            if (cropWidth < 1)
            {
                return;
            }
            Rectangle rect = new Rectangle(cropX, cropY, cropWidth, cropHeight);
            //First we define a rectangle with the help of already calculated points
            Bitmap OriginalImage = new Bitmap(picBoxScreenshot.Image, picBoxScreenshot.Width, picBoxScreenshot.Height);
            //Original image
            Bitmap _img = new Bitmap(cropWidth, cropHeight);
            // for cropinfo image
            Graphics g = Graphics.FromImage(_img);
            // create graphics
            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(OriginalImage, 0, 0, rect, GraphicsUnit.Pixel);

            picBox.Image = _img;
            picBox.Width = _img.Width;
            picBox.Height = _img.Height;
            PictureBoxLocation();
            cropWidth = 0;
        }
        catch (Exception ex){}
    }

 private void PictureBoxLocation()
    {
        int _x = 0;
        int _y = 0;
        if (panel1.Width > picBox.Width)
        {
            _x = (panel1.Width - picBox.Width) / 2;
        }
        if (panel1.Height > picBox.Height)
        {
            _y = (panel1.Height - picBox.Height) / 2;
        }
        picBox.Location = new Point(_x, _y);
        picBox.Refresh();
    }
cosmoloc
  • 2,884
  • 5
  • 29
  • 48
0

The outside of the selection box seems to have a black image laid over it with a alpha of about 30%. To do this you would just take each pixel outside of the content area and draw a black pixel with a 30% alpha on top of it. This would give the desired dimmed out effect.

As for how you can get a rectangle to be dynamically seizable in C#.

Bobby
  • 11,419
  • 5
  • 44
  • 69
Nick Berardi
  • 54,393
  • 15
  • 113
  • 135
  • Thanks Nick Beradi for help. the problem is that if i do work on one pixel one by one, its take very time . is there any fast way like color matrix? – qulzam Aug 03 '09 at 01:27
0

In order to draw a picture lighter or darker (or alter the colors in any way) you use a ColorMatrix, like this.

Dan Byström
  • 9,067
  • 5
  • 38
  • 68
  • i know the use of color matrix but i know only use the colormatrix on all the image. how can we use it on the part of the image? – qulzam Aug 03 '09 at 16:21
  • Draw it as five seprate rectangles (five calls to .DrawImage). You can divide the picture in four rectangles that covers all that is outside your selection and one rectangle that is the selection in itself. – Dan Byström Aug 04 '09 at 08:52