C#: Simple and functional way to zoom picturebox images with scroll bars
Asked Answered
B

1

14

Is there a simple and functional way to zoom an image in a picturebox including scroll bars?

At the moment, I use a picture box in a panel with auto scroll activated. To zoom, I enlarge the picturebox and move it with the scroll bars on the panel. The problem is, that it behaves strange. For example: If you zoom in to far, the margin between the upper and left form border and the image get's bigger and bigger.

That's the zooming method. I got it from here.

private void ZoomInOut(bool zoom)
    {
        //Zoom ratio by which the images will be zoomed by default
        int zoomRatio = 10;
        //Set the zoomed width and height
        int widthZoom = pictureBox_viewer.Width * zoomRatio / 100;
        int heightZoom = pictureBox_viewer.Height * zoomRatio / 100;
        //zoom = true --> zoom in
        //zoom = false --> zoom out
        if (!zoom)
        {
            widthZoom *= -1;
            heightZoom *= -1;
        }
        //Add the width and height to the picture box dimensions
        pictureBox_viewer.Width += widthZoom;
        pictureBox_viewer.Height += heightZoom;

    }

Any help is appreciated.

Thanks in advance.

Marco

EDIT: Two screenshots of an unzoomed and a zoomed (16 times) image. Pay attention to the margin between the upper border of the image and the upper border of the form. UnzoomedImage ZoomedImage

Blenheim answered 16/8, 2013 at 9:30 Comment(10)
Simple answer: No! The current way you are using is the best choice. I don't really get what the problem with it as you described in your question. I think a screen shot would be good in this case.Zollie
@KingKing I've added two screenshots.Blenheim
It's very strange unless there is some code changing the Top and Left of your PictureBox. You should check the whole code yourself of post it here.Zollie
I've already posted any code regarding the zooming in this program. :(Blenheim
No, I mean there should be other code somewhere else, such as code for handling some other events of your PictureBox, your Panel.Zollie
Your zooming code doesn't change the location of PictureBox, so with all that code, it shouldn't change the distance between the Top border of your PictureBox and the Top border of your Panel.Zollie
The only code I found regarding the picturebox or the panel is the following private void Form1_Resize(object sender, EventArgs e) {pictureBox_viewer.Size = panel_viewer.Size; if (viewerSize != Size.Empty) {viewerSize = pictureBox_viewer.Size;} } It's for fitting the pictureBox to the panel when resizing the form.Blenheim
How is the PictureBox anchored to the Panel? Using the Anchor parameter? Dock? Perhaps this is happening because it's trying to respect one of those.Marjoriemarjory
Anchor: Top, Left | Dock: NoneBlenheim
I found the solution for that: set Anchor property of the pictureBox to Top, Bottom, Left. It doesn't make much sence to me, but it works in my case. Two years too late but hope it helps to someone else :)Plotkin
I
4

I think its better to zoom(rescale) the image and not the picture box. Take a look at this article - http://www.codeproject.com/Articles/21097/PictureBox-Zoom

And

How to zoom in&out an image in c#

Impertinence answered 16/8, 2013 at 9:36 Comment(5)
Okay. And when i rescale the image, how can I fit it in the form? I need to update the size of the picture box which leads me to my first problem. Am I right? :)Blenheim
When the image is rescaled, what is required? PAN (where the user can scroll the image using mouse drag) or scrollbars in the picturebox?Impertinence
And what is the problem with the 2nd image? It seems it is zoomed and looks ok.Impertinence
1st question: Scrollbars are required.Blenheim
2nd question: The image is only the white part... The gray part is the background. The Problem is, that the image goes away from the border of the form.Blenheim

© 2022 - 2024 — McMap. All rights reserved.