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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

18 Experts available now in Live!

Get 1:1 Help Now