Resize a picture on import

jimday1982

Member
Joined
Jul 30, 2003
Messages
22
Location
Va Beach
I am importing a picture that needs to be resized to fit a picture box. I know I could use autosize, but that will not work in this instance. Does anyone know of a way to actually change the dimensions of a picture right after the user selects the picture to import to a specific size, such as 100 x 100. The code I currently have is below. Any help is greatly appreciated!

Code:
Private Sub btnPic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPic.Click
        Open dialog box to import a picture

        With ofd
            .CheckFileExists = True
            .CheckPathExists = True
            .Filter = "Images|*.bmp;*.tiff;*.jpg;*.jpeg;*.png;"
            .Multiselect = False
            .ShowHelp = False
            .Title = "Select an Image"
        End With

        If ofd.ShowDialog() = DialogResult.OK Then
            Dim tmpBMP As New Bitmap(ofd.FileName)
            CoverPic.Image = tmpBMP
        End If
    End Sub
 
You could give your bitmap a broader scope and do the following:

Code:
Private Sub Your_Event(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Whatever
  If ofd.ShowDialog = DialogResult.OK Then
    b = new bitmap(ofd.FileName)
    pbImage.Invalidate
  End If
End Sub

Private Sub pbImage_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pbImage.Paint
  If Not (b Is Nothing) Then e.Graphics.DrawImage(DirectCast(b, Drawing.Image), pbimage.ClientRectangle)
End Sub
 
This is very easy in .net. Just create a new Bitmap and specify the dimentions desired there.
 
Back
Top