Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
404 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 400 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 86

Accepted Solution

by:
Mike Tomlinson earned 1600 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 86

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

885 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