On my FormLoad image is load from path in picturebox1 and Image dimension is 12000X24000 when i scroll up from mouse for Zooming at a one point Give error Out of memory and I want to Zooming maximum till the when i can see pixel of the image
private void PictureBox1_MouseWheel(object sender, MouseEventArgs e) { // Adjust the zoom factor as desired float zoomFactor = 2.1f; // Increase or decrease this value to control the zoom level
// Get the cursor position relative to the PictureBox
Point cursorPosition = pictureBox1.PointToClient(Cursor.Position);
if (e.Delta > 0)
{
// Zoom in
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
// Calculate the new width and height based on the zoom factor
int newWidth = (int)(pictureBox1.Width * zoomFactor);
int newHeight = (int)(pictureBox1.Height * zoomFactor);
// Calculate the delta between the new and old dimensions
int deltaWidth = newWidth - pictureBox1.Width;
int deltaHeight = newHeight - pictureBox1.Height;
// Calculate the new location based on the cursor position
int newX = pictureBox1.Left - (int)(deltaWidth * ((float)cursorPosition.X / pictureBox1.Width));
int newY = pictureBox1.Top - (int)(deltaHeight * ((float)cursorPosition.Y / pictureBox1.Height));
// Adjust the size and location of the PictureBox
pictureBox1.Width = newWidth;
pictureBox1.Height = newHeight;
pictureBox1.Left = newX;
pictureBox1.Top = newY;
}
else
{
// Zoom out
if (pictureBox1.Width > originalWidth && pictureBox1.Height > originalHeight)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
// Calculate the new width and height based on the zoom factor
int newWidth = (int)(pictureBox1.Width / zoomFactor);
int newHeight = (int)(pictureBox1.Height / zoomFactor);
// Calculate the delta between the new and old dimensions
int deltaWidth = pictureBox1.Width - newWidth;
int deltaHeight = pictureBox1.Height - newHeight;
// Calculate the new location based on the cursor position
int newX = pictureBox1.Left + (int)(deltaWidth * ((float)cursorPosition.X / pictureBox1.Width));
int newY = pictureBox1.Top + (int)(deltaHeight * ((float)cursorPosition.Y / pictureBox1.Height));
// Adjust the size and location of the PictureBox
pictureBox1.Width = newWidth;
pictureBox1.Height = newHeight;
pictureBox1.Left = newX;
pictureBox1.Top = newY;
}
}
}