I am using a PictureBox to display images. I have SizeMode set to StretchImage. While this does indeed show the whole image, it is of coure stretched. Is there a way to prepare the image to be displayed so it will fit correctly in the PictureBox? I just want it displayed without distortion and within the box. Then perhaps I could set the SizeMode to Normal.

Thanks,
Sheryl

You have to scale the image. The code below gets an image from the file and rescales its size down to fit in the PictureBox.

' See http://www.vb-helper.com/howto_net_image_resize.html for the original code
Dim PicBoxHeight As Integer
Dim PicBoxWidth As Integer
Dim ImageHeight As Integer
Dim ImageWidth As Integer
Dim TempImage As Image
Dim scale_factor As Single

PictureBox1.SizeMode = PictureBoxSizeMode.Normal
' Get control size
PicBoxHeight = PictureBox1.Height
PicBoxWidth = PictureBox1.Width
 ' Load the image. Change Image.FromFile() to match your code
TempImage = Image.FromFile("D:\Download\Image00b.jpg")
' Get image size
ImageHeight = TempImage.Height
ImageWidth = TempImage.Width
' Calculate scale_factor
scale_factor = 1.0 ' No scaling by default i.e. image fits in the Box
' 1) Height
If ImageHeight > PicBoxHeight Then
  ' Reduce height first
  scale_factor = CSng(PicBoxHeight / ImageHeight)
End If
' 2) Width. Notice, we do know now how much we have to scale down the height
' and the scale_factor affects width too
If (ImageWidth * scale_factor) > PicBoxWidth Then
  ' Scaled width exceeds Box's width, recalculate scale_factor
  scale_factor = CSng(PicBoxWidth / ImageWidth)
End If

' Move image to control for resizing
PictureBox1.Image = TempImage

' Get the source bitmap.
Dim bm_source As New Bitmap(PictureBox1.Image)
' Make a bitmap for the result.
Dim bm_dest As New Bitmap( _
    CInt(bm_source.Width * scale_factor), _
    CInt(bm_source.Height * scale_factor))
' Make a Graphics object for the result 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 result.
PictureBox1.Image = bm_dest

I tested the code with a 100x100 px box. The code doesn't scale images up to match PictureBox's size, only down.

commented: This is great. Just what I needed. +0

Wow, it really works -- and there were no errors in your code.

Thank you!

Hello, i've tried the sample code..above however this code works only in a single image..i am doing application which needs a resize module for all the images i've loaded using the radiobuttons.. what i want is that when the user clicks on the images in the radiobuttons it could be resize..say i click pic1 then resize, then i click pic2 again i could resize it..any idea how to do this?

Using Open,save and Crop image

Private Sub photobutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles photobutton.Click
        Dim PicBoxHeight As Integer
        Dim PicBoxWidth As Integer
        Dim ImageHeight As Integer
        Dim ImageWidth As Integer

        Dim scale_factor As Single
        Dim resul As DialogResult = OpenFileDialog1.ShowDialog

        If resul = Windows.Forms.DialogResult.OK Then
            ' Load the image. Change Image.FromFile() to match your code
            imagesbox.Image = Image.FromFile(OpenFileDialog1.FileName)


            imagesbox.SizeMode = PictureBoxSizeMode.Normal
            ' Get control size
            PicBoxHeight = imagesbox.Height
            PicBoxWidth = imagesbox.Width


            ' Get image size
            ImageHeight = imagesbox.Image.Height
            ImageWidth = imagesbox.Image.Width
            ' Calculate scale_factor
            scale_factor = 1.0 ' No scaling by default i.e. image fits in the Box
            ' 1) Height
            If ImageHeight > PicBoxHeight Then
                ' Reduce height first
                scale_factor = CSng(PicBoxHeight / ImageHeight)
            End If
            ' 2) Width. Notice, we do know now how much we have to scale down the height
            ' and the scale_factor affects width too
            If (ImageWidth * scale_factor) > PicBoxWidth Then
                ' Scaled width exceeds Box's width, recalculate scale_factor
                scale_factor = CSng(PicBoxWidth / ImageWidth)
            End If

            ' Move image to control for resizing
            imagesbox.Image = imagesbox.Image

            ' Get the source bitmap.
            Dim bm_source As New Bitmap(imagesbox.Image)
            ' Make a bitmap for the result.
            Dim bm_dest As New Bitmap( _
            CInt(bm_source.Width * scale_factor), _
            CInt(bm_source.Height * scale_factor))
            ' Make a Graphics object for the result 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 result.
            imagesbox.Image = bm_dest

            TextBox10.Text = OpenFileDialog1.FileName
        End If
        If Not (imagesbox.Image) Is Nothing Then
            Dim sfd As New SaveFileDialog
            sfd.Title = "Save your Image picture as...."
            sfd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyPictures
            sfd.Filter = "Jpeg files (*.jpg)|*.jpg|Bitmap files (*.bmp)|*.bmp"

            Dim result As DialogResult = sfd.ShowDialog
            'Dim result As DialogResult
            If result = Windows.Forms.DialogResult.OK Then
                If sfd.FileName <> String.Empty Then
                    'Set to Jpeg by default.
                    Dim MyImageFormat As System.Drawing.Imaging.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg
                    If sfd.FileName.ToString.ToUpper.EndsWith("JPG") Then
                        MyImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg
                        TextBox6.Text = sfd.FileName
                    ElseIf sfd.FileName.ToString.ToUpper.EndsWith("BMP") Then
                        MyImageFormat = System.Drawing.Imaging.ImageFormat.Bmp
                        TextBox6.Text = sfd.FileName
                    End If
                    imagesbox.Image.Save(sfd.FileName, MyImageFormat)

                End If
            End If
        End If
    End Sub
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.