0

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;
            }
        }
    }
  • hey @Jimi can you see this – Mayank Chauhan Jun 15 '23 at 05:24
  • The image is too large. To work on a Bitmap, its content must reside in memory. There's a limitation there, this bit-map must occupy a contiguous space in your Process memory. Each color information takes 3 or 4 bytes and the stride is always aligned to 4 bytes. With an image of that size, out of memory is guaranteed -- Read it as a stream and resize it to a *compatible* size before you work on it (btw, e.g., PhotoShop does this same thing) – Jimi Jun 15 '23 at 06:13
  • still give out of memory – Mayank Chauhan Jun 15 '23 at 06:26
  • This method is not working @Jimi – Mayank Chauhan Jun 15 '23 at 06:41
  • 1
    *Still gives out of memory* after you have done what, exactly? -- What *method* is not working? -- Have you checked what I'm doing here: [Zoom and translate an Image from the mouse location](https://stackoverflow.com/a/61964222/7444103)? For sure not resizing a Control. The size of the image I'm working with in that example is 2560x1700. In memory, its size is ~17.4 MB. When the size of the bitmap in memory goes beyond 540MB, you may get an out of memory exception at any time. May go up to 700MB, but you cannot count on it – Jimi Jun 15 '23 at 07:10
  • Out of memory seems to be because your image pixels are too large. You can load the original image to zoom in and out by creating a new bitmap representing the visible portion of the image at the current zoom level. – wenbingeng-MSFT Aug 31 '23 at 03:28

0 Answers0