Resize memory stream image

Experts, I am using the code below to allow the user to insert an image into a datatable. The datagridview displays part numbers, pricing and description. I want the user to be able to add an image for that item. I want to resize the image that the user selects with a savefiledialog. I need to have a fixed height and resize the width proportionally.

Can you please help me with this.
        Dim sd As New SaveFileDialog()

        sd.Title = "Select product image"
        sd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
        sd.Filter = "Bitmap|*.bmp|JPEG|*.jpg|Gif|*.gif|PNG|*.png"
        sd.FilterIndex = 2
        sd.RestoreDirectory = True

        If sd.ShowDialog() = DialogResult.OK Then
            If DataGridView1.SelectedRows.Count > 0 Then
                Dim intID As Integer = CInt(DataGridView1.SelectedRows(0).Cells("ID").Value)
                Dim row As DataRow = ds.Tables("dtTTOMaster").Rows.Find(intID)
                Using ms As New MemoryStream
                    Using fs As FileStream = File.OpenRead(sd.FileName)
                        ms.SetLength(fs.Length)
                        fs.Read(ms.GetBuffer(), 0, CInt(Fix(fs.Length)))
                    End Using
                    row("PartImage") = ms.ToArray
                End Using
            End If
        End If

Open in new window

BasicfarmerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodeCruiserCommented:
Here is a function you can use


Public Shared Function ScaleImage(image As Image, maxWidth As Integer, maxHeight As Integer) As Bitmap
	Dim ratioX = CDbl(maxWidth) / image.Width
	Dim ratioY = CDbl(maxHeight) / image.Height
	Dim ratio = Math.Min(ratioX, ratioY)

	Dim newWidth = CInt(image.Width * ratio)
	Dim newHeight = CInt(image.Height * ratio)

	Dim newImage = New Bitmap(newWidth, newHeight)
	Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight)
	Dim bmp As New Bitmap(newImage)

	Return bmp
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BasicfarmerAuthor Commented:
Thanks for the quick response....
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.