:: AForge.NET Framework :: Articles :: Forums ::

Cropping Invalid Parameter(Solved)

Forum to discuss AForge.NET Framework, its features, API, how-tos, etc.

Cropping Invalid Parameter(Solved)

Postby nathan2195 » Fri Jun 30, 2017 8:17 pm

Hello, I been having issues trying to get the Crop class to work. The error I am getting is a Invalid parameter exception. I have tried looking it up but I kept going in circles so I was hoping someone here could help me.
The following is the code:
Code: Select all
          // create filter
                    var toCrop = new System.Drawing.Rectangle((int)firstP.X, (int)firstP.Y, (int)(firstP.X-secondP.X), (int)(firstP.Y - secondP.Y));
                    var imgSize = new System.Drawing.Rectangle(0, 0, toRead.Width, toRead.Height);
                    Crop filter = new Crop(toCrop);
                    // apply the filter
                    System.Drawing.Image clonedImg = new Bitmap(toRead.Width, toRead.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                    clonedImg = (System.Drawing.Image)toRead.Clone(imgSize, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                    var bmp8bpp = Grayscale.CommonAlgorithms.BT709.Apply((Bitmap)clonedImg);
                    Bitmap newImage = filter.Apply((Bitmap)bmp8bpp.Clone());

The following is the error detials:
error.PNG (11.95 KiB) Viewed 8548 times

If you need anymore information just ask.

Last edited by nathan2195 on Fri Jun 30, 2017 10:18 pm, edited 1 time in total.
Posts: 2
Joined: Fri Jun 30, 2017 8:06 pm

Re: Cropping Invalid Parameter

Postby andrew.kirillov » Fri Jun 30, 2017 9:11 pm


It looks like there is nothing wrong with Crop. The exception suggests it was thrown by Bitmap constructor. So check what parameters you pass to it. Maybe you pass negative image width/height, for example.
With best regards,

Interested in supporting AForge.NET Framework?
User avatar
Site Admin, AForge.NET Developer
Posts: 3453
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: Cropping Invalid Parameter

Postby nathan2195 » Fri Jun 30, 2017 10:18 pm


Thanks for the help. It turns out that I had the first and second coordinates swapped resulting in a negative height and width. I works now that I swapped them.

Posts: 2
Joined: Fri Jun 30, 2017 8:06 pm

Return to AForge.NET Framework