how can i zoom an image inside a picturebox? and i also want to know how can i make a picture inside the picturebox draggable while at the same time, it can be zoomed in and out.

my goal is to crop an image inside the picturebox and i can zoom and drag the image inside if necessary, thanks :)

Member Avatar for Unhnd_Exception

Uncommented but free code to draw a zoomed image in a picture box with a trackbar. :)

Public Class Form1
    Private TheImageOpened As Bitmap
    Private TheImageRectThatCanBeManipulated As Rectangle
    Private TrackBarCenter As Integer = 8

    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        TrackBar1.Minimum = 1
        TrackBar1.Maximum = 16
        TrackBar1.Value = TrackBarCenter
    End Sub

    Private Sub ButtonOpenTheImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOpenTheImage.Click
        Dim OpenFileDialog As New OpenFileDialog
        OpenFileDialog.Filter = "Image Files|*.png;*.jpg;*.bmp"
        OpenFileDialog.FilterIndex = 0
        OpenFileDialog.RestoreDirectory = True 'Fix Bad logic in Xp that will cause 
        'Environment.CurrentDirectory
        'My.Application.Info.DirectoryPath
        'to retrieve the directory of the path opened from this dialog instead of
        'the app directory.
        If OpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            TheImageOpened = New Bitmap(OpenFileDialog.FileName)
            TheImageRectThatCanBeManipulated = New Rectangle(Point.Empty, TheImageOpened.Size)
            PictureBox1.Invalidate()
        End If

        OpenFileDialog.Reset()
        OpenFileDialog.Dispose()
    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        If TheImageOpened IsNot Nothing Then
            e.Graphics.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
            e.Graphics.DrawImage(TheImageOpened, TheImageRectThatCanBeManipulated)
        End If
    End Sub

    Private Sub TrackBar1_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar1.ValueChanged
        If TheImageOpened IsNot Nothing Then

            TheImageRectThatCanBeManipulated.Width = TheImageOpened.Width * (TrackBar1.Value / TrackBarCenter)
            TheImageRectThatCanBeManipulated.Height = TheImageOpened.Height * (TrackBar1.Value / TrackBarCenter)

            PictureBox1.Invalidate()
        End If
    End Sub
End Class
commented: bull's eye code! :) +0

OMG Unhnd_Exception, that was great! thanks! theres one more thing, how can i make the image inside the picturebox draggable while it can be zoomed in and out? because i want a certain part of the image cropped and zoomed, thanks for your answer! youre an angel! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.