Как обрезать изображение с помощью выделения мышью в picturebox в форме windows
Обрезка изображения-это неправильно. Где я ошибаюсь?
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; } } }
Что я уже пробовал:
Cropping image is wrong. Where I'm wrong?
Patrice T
Тщательно продуманная "обрезка изображения-это неправильно"