Salut,
folgender Source sollte dir weiterhelfen:
' starting point of mouse movement
Private startPoint As Point
' image displayed
Private myImage As Image
' currently displayed rectangle of an image
Private curViewRect As Rectangle
' create a rectangle of two points
Private Function RectFromPoints(ByVal point1 As Point, _
ByVal point2 As Point) As Rectangle
Dim a As Rectangle = Rectangle.FromLTRB( _
Math.Min(point1.X, point2.X), _
Math.Min(point1.Y, point2.Y), _
Math.Max(point1.X, point2.X), _
Math.Max(point1.Y, point2.Y))
Return a
End Function
Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PictureBox1.MouseDown
Select Case e.Button
Case Windows.Forms.MouseButtons.Left
' start select zooming area
startPoint = e.Location
Case Windows.Forms.MouseButtons.Right
' show the complete picture
curViewRect = New Rectangle(0, 0, _
myImage.Size.Width, myImage.Size.Height)
PictureBox1.Refresh()
End Select
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PictureBox1.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
' draw rectangle
Dim g As Graphics = PictureBox1.CreateGraphics()
PictureBox1.Refresh()
g.DrawRectangle(Pens.Red, RectFromPoints(startPoint, e.Location))
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles PictureBox1.MouseUp
If e.Button = Windows.Forms.MouseButtons.Left Then
' if starting end end point coincide, cancel zooming
If e.Location = startPoint Then
Exit Sub
End If
Dim factorX, factorY As Double
factorX = curViewRect.Width / PictureBox1.ClientRectangle.Width
factorY = curViewRect.Height / PictureBox1.ClientRectangle.Height
Dim endPoint As Point = e.Location
startPoint.X = startPoint.X * factorX
startPoint.Y = startPoint.Y * factorY
endPoint.X = endPoint.X * factorX
endPoint.Y = endPoint.Y * factorY
startPoint.Offset(curViewRect.Location)
endPoint.Offset(curViewRect.Location)
Dim a As Rectangle = RectFromPoints(startPoint, endPoint)
' check zooming rectangle to not get out of bounds
If a.X < 0 Then
a.Width = a.Width + a.X
a.X = 0
End If
If a.Y < 0 Then
a.Height = a.Height + a.Y
a.Y = 0
End If
If a.Right > myImage.Size.Width Then
a.Width = myImage.Size.Width - a.Left
End If
If a.Bottom > myImage.Size.Height Then
a.Height = myImage.Size.Height - a.Top
End If
' apply zoom
curViewRect = a
PictureBox1.Refresh()
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
' load image
myImage = Image.FromFile("D:\Stuff\Wallpaper\p0310153.jpg")
' start with complete image
curViewRect = New Rectangle(0, 0, _
myImage.Size.Width - 1, myImage.Size.Height - 1)
End Sub
Private Sub PictureBox1_Paint(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles PictureBox1.Paint
' draw zoomed part
e.Graphics.DrawImage(myImage, PictureBox1.ClientRectangle, _
curViewRect, GraphicsUnit.Pixel)
End Sub Dafür benötigst du eine PictureBox (PictureBox1), die Datei wird in der Form_Load geladen. Vermutlich musst du dann auch noch die Ereignisse von PictureBox1 und Form_Load mit den entsprechenden Ereignissen verknüpfen (im Eigenschaftsfenster unter Ereignisse).
Stefan
Web: http://www.vbtricks.de.vu/
VBTricks.de.vu. Meine Webseite zu VB und anderen Programmiersprachen. Verschiedene fortgeschrittene OCXe und komplette Projekte sind im Sourcecode verf?gbar. |