Arun E

Arun E

  • NA
  • 105
  • 7.3k

how to crop nd save an image in picturebox in strechmode-c#

Oct 20 2016 10:48 AM

I am unable to crop an image in a picturebox in strechmode which i tried for weeks and dint get a solution yet. The problem is my images size is say(1200,750) and the picture size is say(450,300) but when i try to crop it in picturebox in strechmode it takes the coordinates of original size of the image(1200,750) but i need to crop with the coordinates of the image displayed in picturebox(450,300), therefore i am getting faulty results in cropping. I am working with visual studio 2013 in c# winforms. Please provide me with a solution. Thank you in advance. ` private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { // Starting point of the selection: if (e.Button == MouseButtons.Left) { _selecting = true; _selection = new Rectangle(new Point(e.X, e.Y), new Size()); } }

 private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { // Update the actual size of the selection: if (_selecting) {             _selection.Width = e.X - _selection.X;             _selection.Height = e.Y - _selection.Y; // Redraw the picturebox:             pictureBox1.Refresh(); } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { // Make a note that we "have the mouse".          bHaveMouse = true; // Store the "starting point" for this rubber-band rectangle.          ptOriginal.X = e.X;         ptOriginal.Y = e.Y; // Special value lets us know that no previous  // rectangle needs to be erased.  // Display coordinates  //lbCordinates.Text = "Coordinates  :  " + e.X.ToString() + ", " + e.Y.ToString(); //ptLast.X = -1; //ptLast.Y = -1; //rectCropArea = new Rectangle(new Point(e.X, e.Y), new Size());  if (e.Button == MouseButtons.Left && _selecting) { // Create cropped image: Image img = pictureBox1.Image.Crop(_selection); // Fit image to the picturebox:             pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;             pictureBox1.Image = img.Fit2PictureBox(pictureBox1);             _selecting = false; }         button5.Enabled = true; } private void pictureBox1_Paint(object sender, PaintEventArgs e) { if (_selecting) { // Draw a rectangle displaying the current selection Pen pen = Pens.GreenYellow;             e.Graphics.DrawRectangle(pen, _selection); } }

the namespace class i used is as followsnamespace gfoidl.Imaging {

public static class ImageExtension { public static Image Crop(this Image image, Rectangle selection) { Bitmap bmp = image as Bitmap; // Check if it is a bitmap: if (bmp == null) throw new ArgumentException("Kein gültiges Bild (Bitmap)"); // Crop the image: Bitmap cropBmp = bmp.Clone(selection, bmp.PixelFormat); // Release the resources:         image.Dispose(); return cropBmp; } public static Image Fit2PictureBox( this Image image, PictureBox picBox) { Bitmap bmp = null; Graphics g; // Scale: double scaleY = (double)image.Width / picBox.Width; double scaleX = (double)image.Height / picBox.Height; double scale = scaleY < scaleX ? scaleX : scaleY; // Create new bitmap:         bmp = new Bitmap( (int)((double)image.Width / scale), (int)((double)image.Height / scale)); // Set resolution of the new image:         bmp.SetResolution(             image.HorizontalResolution,             image.VerticalResolution); // Create graphics:         g = Graphics.FromImage(bmp); // Set interpolation mode: //g.InterpolationMode = InterpolationMode.HighQualityBicubic; // Draw the new image:          g.DrawImage(             image, new Rectangle( // Ziel 0, 0,                 bmp.Width, bmp.Height), new Rectangle( // Quelle 0, 0,                 image.Width, image.Height), GraphicsUnit.Pixel);         picBox.SizeMode = PictureBoxSizeMode.StretchImage; // Release the resources of the graphics:         g.Dispose(); // Release the resources of the origin image:         image.Dispose(); return bmp; } }

}` invoked it in the main program with the header using gfoid1.imaging;


Answers (2)