1,105,177 Community Members

zooming an image in a picturebox using trackbar

Member Avatar
kazekagerandy
Junior Poster in Training
58 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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 :)

Unhnd_Exception
Deleted Member
 
3
 

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
Member Avatar
kazekagerandy
Junior Poster in Training
58 posts since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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! :)

Question Answered as of 3 Years Ago by Unhnd_Exception
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: