FAQ: How do I resize an image by the same scale?

Locked FAQ: How do I resize an image by the same scale?

Locked

  • Friday, April 10, 2009 2:55 PM
     
      Has Code

    How do I resize an image by the same scale?


    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

  • Friday, April 10, 2009 2:56 PM
     
     Answered Has Code

    The main idea: Create a new Bitmap with a new size, then draw the 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://social.msdn.microsoft.com/forums/en-US/vbide/thread/69bae6d7-eb4e-49cd-b078-704a028e9318

     

    For more FAQ about Visual Basic .NET General, please see Visual Basic .NET General FAQ


    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.
  • Wednesday, May 20, 2009 11:26 AM
     
      Has Code

    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 = "D:\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

    Trackback: http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/50d2ad98-405e-4943-a300-021fd3ecbaa4
    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.
    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.