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
396 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

772 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