Click here to Skip to main content
15,886,085 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Cropping image is wrong. Where I'm wrong?



C#
        int cropX;
        int cropY;
        int cropWidth;
        int cropHeight;
        public Pen cropPen;

        public DashStyle cropDashStyle = DashStyle.DashDot;
        public bool Makeselection = false;

<pre>private void button4_Click(object sender, EventArgs e)
        {
            Makeselection = true;
            button4.Enabled = true;

            Cursor = Cursors.Default;

            try
            {
                if (cropWidth < 1)
                {
                    return;
                }
                Rectangle rect = new Rectangle(cropX, cropY, cropWidth, cropHeight);
                Bitmap OriginalImage = new Bitmap(pictureBox1.Image, pictureBox1.Width, pictureBox1.Height);
                Bitmap _img = new Bitmap(cropWidth, cropHeight);
                Graphics g = Graphics.FromImage(_img);

                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.DrawImage(OriginalImage, 0, 0, rect, GraphicsUnit.Pixel);

                pictureBox1.Image = _img;
                pictureBox1.Width = _img.Width;
                pictureBox1.Height = _img.Height;
            }
            catch (Exception ex)
            {

            }
        }

        private void pictureBox1_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.Red, 1);
                            cropPen.DashStyle = DashStyle.DashDotDot;


                        }                       
                        pictureBox1.Refresh();
                    }
                    catch (Exception ex)
                    {

                    }
                }
            
        }

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

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

                    try
                    {
                        if (pictureBox1.Image == null)
                            return;


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



                    }
                    catch (Exception ex)
                    {
                        //if (ex.Number == 5)
                        //    return;
                    }
                }
            
        }


What I have tried:

Cropping image is wrong. Where I'm wrong?
Posted
Updated 27-Feb-19 21:48pm
Comments
Patrice T 27-Feb-19 18:47pm    
Elaborate "Cropping image is wrong"

1 solution

Probably, it's scaling. Check your PictureBox settings, specifically the PictureBox.SizeMode Property (System.Windows.Forms) | Microsoft Docs[^] - if it isn't set to "Normal" then the mouse coordinates you are using are relative to the Picturebox but not the the image it contains. If the image is scaled at all, then when you use mouse coordinates at all they don't match the actual picture you are clipping from.

A couple of other things:
1) Graphics contexts are scarce resources - and you are responsible for Disposing of them when you create them. If you don't, you will get "out of memory" errors long before the GC gets called in to tidy up after you. A using block is the best way, as it automatically Disposes when the object goes out of scope, regardless of how it exits the block.
C#
using (Graphics g = Graphics.FromImage(_img))
   {
   g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
   ...
   }

2) Why are you creating so many bitmaps? You don't need half of them!
3) Never swallow exceptions:
catch (Exception ex)
{

}
When you do that, you throw away all information that might help you fix a problem later, as well as "hiding" the problem so you don't even know it happened, and can't fix it early enough to not make a proper mess. Log 'em, Show 'em to the user, display them on the Debug console - whatever works for you. But don;t just blanket swallow them!
 
Share this answer
 

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