private void menuItemCrop_Click(object sender, EventArgs e) { CropForm cpFrm = new CropForm(); cpFrm.CropXPosition = 0; cpFrm.CropYPosition = 0; cpFrm.CropWidth = imageHandler.CurrentBitmap.Width; cpFrm.CropHeight = imageHandler.CurrentBitmap.Height; if (cpFrm.ShowDialog() == DialogResult.OK) { this.Cursor = Cursors.WaitCursor; imageHandler.RestorePrevious(); imageHandler.DrawOutCropArea(cpFrm.CropXPosition, cpFrm.CropYPosition, cpFrm.CropWidth, cpFrm.CropHeight); this.Invalidate(); if (MessageBox.Show("Do u want to crop this area?", "ImageProcessing", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { imageHandler.Crop(cpFrm.CropXPosition, cpFrm.CropYPosition, cpFrm.CropWidth, cpFrm.CropHeight); } else { imageHandler.RemoveCropAreaDraw(); } this.Invalidate(); this.Cursor = Cursors.Default; } }
private void menuItemCrop_Click(object sender, EventArgs e) { var cpFrm = new CropForm(); cpFrm.CropXPosition = 0; ClientSize = imageHandler.GetBitmapSize(); cpFrm.CropYPosition = 0; cpFrm.CropWidth = imageHandler.CurrentBitmap.Width; cpFrm.CropHeight = imageHandler.CurrentBitmap.Height; if (cpFrm.ShowDialog() == DialogResult.OK) { Cursor = Cursors.WaitCursor; imageHandler.RestorePrevious(); imageHandler.DrawOutCropArea(cpFrm.CropXPosition, cpFrm.CropYPosition, cpFrm.CropWidth, cpFrm.CropHeight); Invalidate(); if (MessageBox.Show("Do u want to crop this area?", "ImageProcessing", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { if (cpFrm.CropWidth < Screen.PrimaryScreen.Bounds.Width && cpFrm.CropHeight < Screen.PrimaryScreen.Bounds.Height) { imageHandler.Crop(cpFrm.CropXPosition, cpFrm.CropYPosition, cpFrm.CropWidth, cpFrm.CropHeight); AutoScroll = false; WindowState = FormWindowState.Normal; ClientSize = imageHandler.GetBitmapSize(); } else if (cpFrm.CropWidth > Screen.PrimaryScreen.Bounds.Width && cpFrm.CropHeight < Screen.PrimaryScreen.Bounds.Height) { imageHandler.Crop(cpFrm.CropXPosition, cpFrm.CropYPosition, cpFrm.CropWidth, cpFrm.CropHeight); WindowState = FormWindowState.Normal; ClientSize = imageHandler.GetBitmapSize(); } else if (cpFrm.CropWidth < Screen.PrimaryScreen.Bounds.Width && cpFrm.CropHeight > Screen.PrimaryScreen.Bounds.Height) { imageHandler.Crop(cpFrm.CropXPosition, cpFrm.CropYPosition, cpFrm.CropWidth, cpFrm.CropHeight); WindowState = FormWindowState.Normal; ClientSize = imageHandler.GetBitmapSize(); } else { ClientSize = imageHandler.GetBitmapSize(); } } else { imageHandler.RemoveCropAreaDraw(); } Invalidate(); Cursor = Cursors.Default; } }
private void menuItemCrop_Click(object sender, EventArgs e) //функция вызывается при выделении области { bool flag = false; //объявления флага на случай выхода области выделения за приделы изображения CropForm cpFrm = new CropForm(); //создаем форму для определения параметров выделения do { flag = false; cpFrm.CropXPosition = 0; cpFrm.CropYPosition = 0; cpFrm.CropWidth = imageHandler.Width; //отображаем на форме выделения ширину cpFrm.CropHeight = imageHandler.Height; //и высоту изображения для удобства определения параметров if (cpFrm.ShowDialog() == DialogResult.OK) { Rectangle rec = new Rectangle(cpFrm.CropXPosition, cpFrm.CropYPosition, cpFrm.CropWidth, cpFrm.CropHeight); //создаем прямоугольник с саданными параметрами выделения this.Cursor = Cursors.WaitCursor; //курсор в режиме ожидания imageHandler.RestorePrevious(); //точка сохранения для возврата imageHandler.DrawOutCropArea(cpFrm.CropXPosition, cpFrm.CropYPosition, cpFrm.CropWidth, cpFrm.CropHeight); //вызов функции выделения выбранной области this.Invalidate(); if (MessageBox.Show("Выделить выбранную область?", "ImageProcessing", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { try { cropX = cpFrm.CropXPosition; cropY = cpFrm.CropYPosition; widthImgCrop = cpFrm.CropWidth; heightImgCrop = cpFrm.CropHeight; temp = (Bitmap)imageHandler.GetBitmap(); Bitmap bmap = (Bitmap)temp.Clone(); // копия картинки Graphics gr = Graphics.FromImage(bmap); //создает объект Graphics из картинки RectangleF cloneRect = new RectangleF(cropX, cropY, widthImgCrop, heightImgCrop); // прямоугольник соотв выделеной области System.Drawing.Imaging.PixelFormat format = bmap.PixelFormat; // cloneBitmap = bmap.Clone(cloneRect, format); // вырезка куска из картинке по выделеной области cloneRect pictureBox1.Location = new System.Drawing.Point(cropX, cropY); // устанавливаем pictureBox для вывода картинок в коордиаты Х У т. е. на месте где и была выделена область pictureBox1.Image = cloneBitmap; // помещаем картинку pictureBox1.Size = new System.Drawing.Size(Convert.ToInt32(widthImgCrop), Convert.ToInt32(heightImgCrop)); //размеры штуки для вывода pictureBox1.Visible = true; //pictureBox1 с выделенным изображением становится видимой imageHandler.RemoveCropAreaDraw(); this.saveImgImg.Enabled = true; this.deleteCrop.Enabled = true; } catch (Exception ex) { MessageBox.Show(this, "Выделена область вне картинки!", "Внимание", MessageBoxButtons.OK); flag = true; } } else { imageHandler.RemoveCropAreaDraw(); //отмена выделения выбранной области } this.AutoScroll = true; this.AutoScrollMinSize = new Size(Convert.ToInt32(imageHandler.Width), Convert.ToInt32(imageHandler.Height)); this.Invalidate(); this.Cursor = Cursors.Default; imageHandler.RemoveCropAreaDraw(); this.Invalidate(); } }while(flag == true); this.Invalidate(); this.Cursor = Cursors.Default; }