When i click ok, the bounding box which i have defined should correctly crop the picture. But without changing the dimension of the crop box, and clicking OK, the image is getting cropped wrongly. I dont know why. I am providing the code.
private void CropForm_Load(object sender, EventArgs e)
{
float scaleX = (float)pictureBox1.Image.Width / pictureBox1.ClientSize.Width;
float scaleY = (float)pictureBox1.Image.Height / pictureBox1.ClientSize.Height;
float scaleFactor = Math.Max(scaleX, scaleY);
var imageSize = new Size((int)(pictureBox1.Image.Width / scaleFactor), (int)(pictureBox1.Image.Height / scaleFactor));
int rectWidth = imageSize.Width;
int rectHeight = imageSize.Height;
// Calculate the new position of the bounding rectangle (top left anchor)
int rectX = (pictureBox1.Width - imageSize.Width) / 2;
int rectY = (pictureBox1.Height - imageSize.Height) / 2;
boundingRect = new Rectangle(rectX, rectY, rectWidth, rectHeight);
imageSizeDimension = new Rectangle(rectX, rectY, imageSize.Width, imageSize.Height);
originalImage = pictureBox1.Image;
pictureBox1.MouseDown += pictureBox1_MouseDown;
pictureBox1.MouseMove += pictureBox1_MouseMove;
pictureBox1.MouseUp += pictureBox1_MouseUp;
pictureBox1.MouseLeave += pictureBox1_MouseLeave;
pictureBox1.Invalidate();
}
public Image CropImage(Image originalImage, Rectangle cropRectangle)
{
Console.WriteLine(boundingRect);
Console.WriteLine(imageSizeDimension);
Console.WriteLine(pictureBox1.ClientSize);
// Create a new bitmap with the dimensions of the crop rectangle
Bitmap croppedImage = new Bitmap(cropRectangle.Width, cropRectangle.Height);
// Create a graphics object from the cropped image
using (Graphics g = Graphics.FromImage(croppedImage))
{
// Draw the portion of the original image that falls within the crop rectangle onto the cropped image
g.DrawImage(originalImage, new Rectangle(0, 0, croppedImage.Width, croppedImage.Height),
cropRectangle, GraphicsUnit.Pixel);
}
return croppedImage;
}
private void OkBtn_Click(object sender, EventArgs e)
{
pictureBox1.Image = CropImage(pictureBox1.Image, boundingRect);
float scaleX = (float)pictureBox1.Image.Width / pictureBox1.ClientSize.Width;
float scaleY = (float)pictureBox1.Image.Height / pictureBox1.ClientSize.Height;
float scaleFactor = Math.Max(scaleX, scaleY);
var imageSize = new Size((int)(pictureBox1.Image.Width / scaleFactor), (int)(pictureBox1.Image.Height / scaleFactor));
int rectWidth = imageSize.Width;
int rectHeight = imageSize.Height;
// Calculate the new position of the bounding rectangle (top left anchor)
int rectX = (pictureBox1.Width - imageSize.Width) / 2;
int rectY = (pictureBox1.Height - imageSize.Height) / 2;
boundingRect = new Rectangle(rectX, rectY, rectWidth, rectHeight);
imageSizeDimension = new Rectangle(rectX, rectY, imageSize.Width, imageSize.Height);
pictureBox1.Invalidate();
}
If i click the OK button without changing the bounding rectangle dimension, it the picture should not be cropped. However this is what is happening. Clicking crop multiple times results in this (attached image)