Visual Basic > Visual Basic Forums > Visual Basic General > Picturebox - Fit to Width
Ask a questionAsk a question
 

AnswerPicturebox - Fit to Width

  • Thursday, October 29, 2009 1:27 PMAnilM_Nair Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,
     My requirement is to display multipage tiff images using a PictureBox (in VB.Net (2.0)).
    For that i've created a panel (autoscroll ON)and top of that placed the picturebox and i am able to load the image in its actual size.
     Now i need to implement
    a)Fit to Width b) Size to Fit and Zoom(2.0/1.0/0.75/0.5) option. How can i achieve this ?
    Thanks,
    Anil



Answers

  • Friday, October 30, 2009 8:11 AMJohnWein Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    With PictureBox.SizeMode, how will you implement 'Fit to Width' ? In the current scenario size of the image is greater than the size of Picture box.So while maintaining  the height ratio the width shall fit to the picturebox. 
    software engineer

    Zoom fits to width or to height and maintains the aspect ratio.  If the PictureBox doesn't do what you want, use a panel and do your own drawing of the image, adjusting the graphics transform.
  • Wednesday, November 04, 2009 8:59 AMMartin Xie - MSFTMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Thank you JohnWein for your friendly help.

    Hi Anil,

    Welcome to MSDN forums!

    Besides, here are some tutorials for you to check, which may be helpful to you.

    How to resize image by the same scale?

    The main idea: Create a new Bitmap with new size, then draw original Bitmap on the new Bitmap object.

    Prerequisites: Drag&drop Button1, PictureBox1 and PictureBox2 onto Form1.

    Public Class Form1

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            PictureBox1.ImageLocation = "D:\mm.jpg"

            PictureBox2.ImageLocation = "D:\mmm.jpg"

        End Sub

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            resizeImage(PictureBox1.ImageLocation, PictureBox1)

            resizeImage(PictureBox2.ImageLocation, PictureBox2)

        End Sub

     

        Public Sub resizeImage(ByVal imgPath As String, ByVal picbox As PictureBox)

            ' Get the source bitmap  

            Dim bm_source As Bitmap = New Bitmap(imgPath)

            ' Record the scale of source bitmap  

            Dim scale As Double = bm_source.Height / bm_source.Width

            ' Make a new bitmap  

            Dim bm_dest As New Bitmap(CInt(picbox.Width), CInt(picbox.Width * scale))

            ' Make a Graphics object for the new Bitmap.  

            Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

            ' Copy the source image into the destination bitmap  

            gr_dest.DrawImage(bm_source, 0, 0, bm_dest.Width + 1, bm_dest.Height + 1)

            ' Display the new bitmap in PictureBox  

            picbox.Image = bm_dest

        End Sub

    End Class

    Related thread:

    http://forums.msdn.microsoft.com/en-US/vbide/thread/69bae6d7-eb4e-49cd-b078-704a028e9318  

     


    Additionally, here is one better approach to zoom image in PictureBox by scrolling mouse wheel.

    Prerequisites: Drag&drop PictureBox1 onto Form1.

    Public Class Form1

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            PictureBox1.SizeMode = PictureBoxSizeMode.Zoom

            PictureBox1.ImageLocation = "E:\VBProject\1.jpg"

        End Sub

     

        Private Sub PictureBox1_MouseWheel(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseWheel

            If e.Delta <> 0 Then

                If e.Delta <= 0 Then

                    If PictureBox1.Width < 50 Then Exit Sub 'minimum 50?

                Else

                    If PictureBox1.Width > 500 Then Exit Sub 'maximum 500?

                End If

                PictureBox1.Width += CInt(PictureBox1.Width * e.Delta / 1000)

                PictureBox1.Height += CInt(PictureBox1.Height * e.Delta / 1000)

            End If

        End Sub

     

        Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove

            PictureBox1.Select()

        End Sub

     

    End Class

     


    Best regards,
    Martin Xie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

All Replies

  • Thursday, October 29, 2009 1:29 PMJohnWein Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    With the PictureBox.SizeMode property.
  • Friday, October 30, 2009 6:11 AMAnilM_Nair Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    With PictureBox.SizeMode, how will you implement 'Fit to Width' ? In the current scenario size of the image is greater than the size of Picture box.So while maintaining  the height ratio the width shall fit to the picturebox. 
    software engineer
  • Friday, October 30, 2009 8:11 AMJohnWein Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    With PictureBox.SizeMode, how will you implement 'Fit to Width' ? In the current scenario size of the image is greater than the size of Picture box.So while maintaining  the height ratio the width shall fit to the picturebox. 
    software engineer

    Zoom fits to width or to height and maintains the aspect ratio.  If the PictureBox doesn't do what you want, use a panel and do your own drawing of the image, adjusting the graphics transform.
  • Wednesday, November 04, 2009 8:59 AMMartin Xie - MSFTMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Thank you JohnWein for your friendly help.

    Hi Anil,

    Welcome to MSDN forums!

    Besides, here are some tutorials for you to check, which may be helpful to you.

    How to resize image by the same scale?

    The main idea: Create a new Bitmap with new size, then draw original Bitmap on the new Bitmap object.

    Prerequisites: Drag&drop Button1, PictureBox1 and PictureBox2 onto Form1.

    Public Class Form1

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            PictureBox1.ImageLocation = "D:\mm.jpg"

            PictureBox2.ImageLocation = "D:\mmm.jpg"

        End Sub

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            resizeImage(PictureBox1.ImageLocation, PictureBox1)

            resizeImage(PictureBox2.ImageLocation, PictureBox2)

        End Sub

     

        Public Sub resizeImage(ByVal imgPath As String, ByVal picbox As PictureBox)

            ' Get the source bitmap  

            Dim bm_source As Bitmap = New Bitmap(imgPath)

            ' Record the scale of source bitmap  

            Dim scale As Double = bm_source.Height / bm_source.Width

            ' Make a new bitmap  

            Dim bm_dest As New Bitmap(CInt(picbox.Width), CInt(picbox.Width * scale))

            ' Make a Graphics object for the new Bitmap.  

            Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

            ' Copy the source image into the destination bitmap  

            gr_dest.DrawImage(bm_source, 0, 0, bm_dest.Width + 1, bm_dest.Height + 1)

            ' Display the new bitmap in PictureBox  

            picbox.Image = bm_dest

        End Sub

    End Class

    Related thread:

    http://forums.msdn.microsoft.com/en-US/vbide/thread/69bae6d7-eb4e-49cd-b078-704a028e9318  

     


    Additionally, here is one better approach to zoom image in PictureBox by scrolling mouse wheel.

    Prerequisites: Drag&drop PictureBox1 onto Form1.

    Public Class Form1

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            PictureBox1.SizeMode = PictureBoxSizeMode.Zoom

            PictureBox1.ImageLocation = "E:\VBProject\1.jpg"

        End Sub

     

        Private Sub PictureBox1_MouseWheel(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseWheel

            If e.Delta <> 0 Then

                If e.Delta <= 0 Then

                    If PictureBox1.Width < 50 Then Exit Sub 'minimum 50?

                Else

                    If PictureBox1.Width > 500 Then Exit Sub 'maximum 500?

                End If

                PictureBox1.Width += CInt(PictureBox1.Width * e.Delta / 1000)

                PictureBox1.Height += CInt(PictureBox1.Height * e.Delta / 1000)

            End If

        End Sub

     

        Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove

            PictureBox1.Select()

        End Sub

     

    End Class

     


    Best regards,
    Martin Xie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.