Solved

Need to allow the user to zoom different parts of a bigger image by dragging with the mouse

Posted on 2008-06-17
4
395 Views
Last Modified: 2013-12-16
So, I have a Windows Forms app that shows some big pictures like 4000x3000. Normally the picture is showed in Clip mode, so the user sees just a small fraction of it, say 1024x768. So the user should be able to click a button with a Hand icon (you know what I'm talking about) and move the picture around, thus seeing different parts of the picture.

Can you suggest any code that would do that?

I see the picture control can't do this out of the box, and I have some bad experience with trying to draw with GDI+ and not getting what I expect (also there are bugs in GDI+ itself...)
0
Comment
Question by:kerzner
  • 2
4 Comments
 
LVL 16

Assisted Solution

by:CuteBug
CuteBug earned 100 total points
ID: 21800736
Try this link

http://www.codeproject.com/KB/static/Scrollable_Image_Viewer.aspx

This might give you an idea how to implement the scrolling of a displayed image.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 400 total points
ID: 21804299
Place a Panel on you Form.
Set the AutoScroll() Property of the Panel to True.
Place a PictureBox in the Panel.
Set the SizeMode() Property of the PictureBox to AutoSize.
Set the Cursor() Property of the PictureBox to Hand.
Here is some VB.Net code to allow the user to Pan the Image:
Public Class Form1
 

    Private dX, dY As Integer
 

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown

        If e.Button = Windows.Forms.MouseButtons.Left Then

            dX = Cursor.Position.X

            dY = Cursor.Position.Y

        End If

    End Sub
 

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove

        If e.Button = Windows.Forms.MouseButtons.Left Then

            Dim nX As Integer = Panel1.HorizontalScroll.Value + (dX - Cursor.Position.X) / Panel1.Width * (Panel1.HorizontalScroll.Maximum - Panel1.HorizontalScroll.Minimum)

            If nX < Panel1.HorizontalScroll.Minimum Then

                nX = Panel1.HorizontalScroll.Minimum

            End If

            If nX > Panel1.HorizontalScroll.Maximum Then

                nX = Panel1.HorizontalScroll.Maximum

            End If

            Panel1.HorizontalScroll.Value = nX
 

            Dim ny As Integer = Panel1.VerticalScroll.Value + (dY - Cursor.Position.Y) / Panel1.Height * (Panel1.VerticalScroll.Maximum - Panel1.VerticalScroll.Minimum)

            If ny < Panel1.VerticalScroll.Minimum Then

                ny = Panel1.VerticalScroll.Minimum

            End If

            If ny > Panel1.VerticalScroll.Maximum Then

                ny = Panel1.VerticalScroll.Maximum

            End If

            Panel1.VerticalScroll.Value = ny
 

            dX = Cursor.Position.X

            dY = Cursor.Position.Y

        End If

    End Sub
 

End Class

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 21804308
* Make the Location of the PictureBox (0, 0).
0
 
LVL 1

Author Comment

by:kerzner
ID: 21814555
Ok, thanks guys!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Studio - "Windows Form Designer generated code" 2 39
Need to pass a connection through class/Method 1 39
Syntax Error 2 43
Variable Event ? 3 22
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now