private void btnSaveAs_Click(object sender, EventArgs e)
        {
            if (database == null)
            {
                return;
            }

            /*if (!chkUnifiyScales.Checked && !chkRandomize.Checked && !chkInflate.Checked)
             *  MessageBox.Show("Please select at least one option!", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error);*/

            Database newDb = database.Clone();

            //inflate
            if (chkInflate.Checked)
            {
                var inflateFactor = (float)this.nInflateFactor.Value;

                newDb = newDb.ModifyAnnotations(imgAnn =>
                {
                    var rect         = imgAnn.Polygon.BoundingRect();
                    var inflatedRect = (RectangleF)rect.Inflate((double)inflateFactor, inflateFactor);

                    var modifiedImgAnn     = (Annotation)imgAnn.Clone();
                    modifiedImgAnn.Polygon = inflatedRect.Vertices();
                    return(modifiedImgAnn);
                });
            }

            //randomize
            if (chkRandomize.Checked)
            {
                var locationRangeX = new RangeF((float)nLocMinX.Value, (float)nLocMaxX.Value);
                var locationRangeY = new RangeF((float)nLocMinY.Value, (float)nLocMaxY.Value);

                var scaleRangeWidth  = new RangeF((float)nScaleMinWidth.Value, (float)nScaleMaxWidth.Value);
                var scaleRangeHeight = new RangeF((float)nScaleMinHeight.Value, (float)nScaleMaxHeight.Value);

                var numSamples = (int)nSamples.Value;

                newDb = newDb.Randomize
                        (
                    locationRand: new Pair <RangeF>(locationRangeX, locationRangeY),
                    scaleRand: new Pair <RangeF>(scaleRangeWidth, scaleRangeHeight),
                    nRandsPerSample: numSamples
                        );
            }

            //unify scales
            if (chkUnifiyScales.Checked)
            {
                var widthHeightRatio = (float)nWidthHeightRatio.Value;

                newDb = newDb.ModifyAnnotations(imgAnn =>
                {
                    var rect       = imgAnn.Polygon.BoundingRect();
                    var scaledRect = ((RectangleF)rect).ScaleTo(widthHeightRatio, correctLocation: true);

                    var modifiedImgAnn     = (Annotation)imgAnn.Clone();
                    modifiedImgAnn.Polygon = scaledRect.Vertices();
                    return(modifiedImgAnn);
                });
            }

            //move annotation to fit
            var imWidth  = (int)nImageWidth.Value;
            var imHeight = (int)nImageHeight.Value;

            newDb = newDb.ModifyAnnotations(imgAnn =>
            {
                var rect = imgAnn.Polygon.BoundingRect();

                RectangleF translatedRect;
                bool translationExist = rect.MoveToFit(new SizeF(imWidth, imHeight), out translatedRect);

                var modifiedImgAnn     = (Annotation)imgAnn.Clone();
                modifiedImgAnn.Polygon = translatedRect.Vertices();
                return(modifiedImgAnn);
            });

            using (var diag = new SaveFileDialog())
            {
                diag.Filter          = "(*.xml)|*.xml";
                diag.OverwritePrompt = true;

                var result = diag.ShowDialog();
                if (result == DialogResult.OK)
                {
                    newDb.Save(diag.FileName);
                    MessageBox.Show("Saved!", "Done!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
 /// <summary>
 /// Randomizes rectangle position and scale and returns randomized rectangles.
 /// </summary>
 /// <param name="rect">Rectangle.</param>
 /// <param name="locationOffset">Minimum location offset for horizontal and vertical direction.</param>
 /// <param name="sizeOffset">Minimum size offset for horizontal and vertical direction.</param>
 /// <param name="nRandomizedRectangles">Number of randomized rectangles to generate.</param>
 /// <param name="rand">Random generator. If null the instance will be generated.</param>
 /// <returns>Randomized rectangles.</returns>
 public static IEnumerable <RectangleF> Randomize(this RectangleF rect, RangeF locationOffset, RangeF sizeOffset, int nRandomizedRectangles, Random rand = null)
 {
     return(Randomize(rect, new Pair <RangeF>(locationOffset, locationOffset), new Pair <RangeF>(sizeOffset, sizeOffset), nRandomizedRectangles, rand));
 }
 /// <summary>
 /// Randomizes rectangle position and scale and returns randomized rectangles.
 /// </summary>
 /// <param name="rect">Rectangle.</param>
 /// <param name="locationOffset">Minimum location offset for horizontal and vertical direction.</param>
 /// <param name="sizeOffset">Minimum size offset for horizontal and vertical direction.</param>
 /// <param name="nRandomizedRectangles">Number of randomized rectangles to generate.</param>
 /// <param name="rand">Random generator. If null the instance will be generated.</param>
 /// <returns>Randomized rectangles.</returns>
 public static IEnumerable<RectangleF> Randomize(this RectangleF rect, RangeF locationOffset, RangeF sizeOffset, int nRandomizedRectangles, Random rand = null)
 {
     return Randomize(rect, new Pair<RangeF>(locationOffset, locationOffset), new Pair<RangeF>(sizeOffset, sizeOffset), nRandomizedRectangles, rand);
 }
        private void btnSaveAs_Click(object sender, EventArgs e)
        {
            if (database == null)
                return;

            /*if (!chkUnifiyScales.Checked && !chkRandomize.Checked && !chkInflate.Checked)
                MessageBox.Show("Please select at least one option!", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error);*/

            Database newDb = database.Clone();

            //inflate
            if (chkInflate.Checked)
            {
                var inflateFactor = (float)this.nInflateFactor.Value;

                newDb = newDb.ModifyAnnotations(imgAnn =>
                {
                    var rect = imgAnn.Polygon.BoundingRect();
                    var inflatedRect = (RectangleF)rect.Inflate(inflateFactor, inflateFactor);

                    var modifiedImgAnn = (Annotation)imgAnn.Clone();
                    modifiedImgAnn.Polygon = Rectangle.Round(inflatedRect).Vertices();
                    return modifiedImgAnn;
                });
            }

            //randomize
            if (chkRandomize.Checked)
            {
               var locationRangeX = new RangeF((float)nLocMinX.Value, (float)nLocMaxX.Value);
               var locationRangeY = new RangeF((float)nLocMinY.Value, (float)nLocMaxY.Value);

               var scaleRangeWidth = new RangeF((float)nScaleMinWidth.Value, (float)nScaleMaxWidth.Value);
               var scaleRangeHeight = new RangeF((float)nScaleMinHeight.Value, (float)nScaleMaxHeight.Value);

               var numSamples = (int)nSamples.Value;

               newDb = newDb.Randomize
                        (
                            locationRand: new Pair<RangeF>(locationRangeX, locationRangeY),
                            scaleRand: new Pair<RangeF>(scaleRangeWidth, scaleRangeHeight),
                            nRandsPerSample: numSamples
                        );
            }

            //unify scales
            if (chkUnifiyScales.Checked)
            {
                var widthHeightRatio = (float)nWidthHeightRatio.Value;

                newDb = newDb.ModifyAnnotations(imgAnn =>
                {
                    var rect = imgAnn.Polygon.BoundingRect();
                    var scaledRect = ((RectangleF)rect).ScaleTo(widthHeightRatio, correctLocation: true);

                    var modifiedImgAnn = (Annotation)imgAnn.Clone();
                    modifiedImgAnn.Polygon = Rectangle.Round(scaledRect).Vertices();
                    return modifiedImgAnn;
                });
            }

            //clamp
            var imWidth = (int)nImageWidth.Value;
            var imHeight = (int)nImageHeight.Value;

            newDb = newDb.ModifyAnnotations(imgAnn =>
            {
                var rect = imgAnn.Polygon.BoundingRect();
                var clampedRect = rect.Intersect(new Size(imWidth, imHeight), preserveScale: true);

                var modifiedImgAnn = (Annotation)imgAnn.Clone();
                modifiedImgAnn.Polygon = Rectangle.Round(clampedRect).Vertices();
                return modifiedImgAnn;
            });

            using (var diag = new SaveFileDialog())
            {
                diag.Filter = "(*.xml)|*.xml";
                diag.OverwritePrompt = true;

                var result = diag.ShowDialog();
                if (result == DialogResult.OK)
                {
                    newDb.Save(diag.FileName);
                    MessageBox.Show("Saved!", "Done!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }