Click here to Skip to main content
15,903,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to crop a image in circular/Ellipse Format

currently it is cropping the image in rectangle format

iam trying to crop the image by mouse drag

What I have tried:

public Image ScreenShot;
       
        private Image partIamge;
        internal PictureBox picScreenImg;

        private Point mouseDownAt;
        private Point mouseIsAt;


        private bool isMouseDown = false;

        private Color backgroundColor = Color.DarkGray;
        private int backgroundAlpha = 150;
        private Color outlineColor = Color.SkyBlue;
        private int outlineWidth = 1;





private void OnPaint(object sender, PaintEventArgs e)
        {
            SolidBrush opaqueWhiteBrush = new SolidBrush(Color.FromArgb(backgroundAlpha, backgroundColor.R, backgroundColor.G, backgroundColor.B));
            e.Graphics.FillRectangle(opaqueWhiteBrush, 0, 0, picScreenImg.Width, picScreenImg.Height);

            if (isMouseDown)
            {
                Rectangle pos = getMouseMoveRect;
                
                    //e.Graphics.DrawEllipse(new Pen(outlineColor, outlineWidth), new Rectangle(0, 0, pos.Width + outlineWidth, pos.Height + outlineWidth));
                    e.Graphics.DrawEllipse(new Pen(outlineColor, outlineWidth), pos);
                    e.Graphics.DrawImage(partIamge, pos.Location);               

                pnlButtonStrip.Visible = false;
            }
        }

 private void OnMouseDown(object sender, MouseEventArgs e)
        {
            //if (e.Button == MouseButtons.Right && !isMouseDown)
            //{
            //    Application.Exit();
            //}
            //else 
            if (e.Button == MouseButtons.Left)
            {
                mouseDownAt = e.Location;
                Cursor.Position = new Point(e.Location.X + 1, e.Location.Y + 1);
                mouseIsAt = new Point(e.Location.X + 1, e.Location.Y + 1);
                partIamge = ((Bitmap)picScreenImg.Image).Clone(getMouseMoveRect, picScreenImg.Image.PixelFormat);
                isMouseDown = true;
                picScreenImg.Refresh();
            }
        }

        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                mouseIsAt = e.Location;
                pnlButtonStrip.Location = e.Location;
                Rectangle rect = getMouseMoveRect;
                if (rect.Width != 0 && rect.Height != 0)
                {                    
                    partIamge = ((Bitmap)picScreenImg.Image).Clone(rect, picScreenImg.Image.PixelFormat);
                    picScreenImg.Refresh();
                }
            }
            // show the mouse position


        }

        private void OnMouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                bool hasMoved = false;

                if (isMouseDown)
                {
                    hasMoved = true;
                    isMouseDown = false;
                }

                if (partIamge.Width < 100 || partIamge.Height < 100)
                {
                    picScreenImg.Refresh();
                    return;
                }
                

                pnlButtonStrip.Size = new Size(partIamge.Width, pnlButtonStrip.Height);
                pnlButtonStrip.Location = new Point(pnlButtonStrip.Location.X - pnlButtonStrip.Width, pnlButtonStrip.Location.Y + 5);
                pnlButtonStrip.Visible = true;
            }
      }



private Rectangle getMouseMoveRect
        {
            get
            {
                int x = 0;
                int y = 0;
                int width = 0;
                int height = 0;

                if (mouseIsAt.X > mouseDownAt.X)
                {
                    x = mouseDownAt.X;
                    width = mouseIsAt.X - mouseDownAt.X;
                }
                else
                {
                    x = mouseIsAt.X;
                    width = mouseDownAt.X - mouseIsAt.X;
                }

                if (mouseIsAt.Y > mouseDownAt.Y)
                {
                    y = mouseDownAt.Y;
                    height = mouseIsAt.Y - mouseDownAt.Y;
                }
                else
                {
                    y = mouseIsAt.Y;
                    height = mouseDownAt.Y - mouseIsAt.Y;
                }
                //x = 0;
                //y = 0;
                
                return new Rectangle(x, y, width, height);
            }
        }
Posted

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900