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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with error when uploading excel file 3 36
Syntax Issue with SSIS module 26 100
Name Space error VS2015 1 22
Install IIS7.5 on Windows Sever 2012 R2 4 22
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 article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

766 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