ASP.NET VB, Resize my bitmap in memory?

Dim wc2 As New WebClient()
                Dim originalData2 As Byte() = wc2.DownloadData(customurl)
                Dim stream2 As New MemoryStream(originalData2)
                Dim Logo As New Bitmap(stream2)

What are the next lines of code to resize Logo so that it is a maximum of 400 pixels wide (if less than 400, leave alone), and a maximum of 100 pixels high (if less than 100 high, then leave alone)
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Alan WarrenConnect With a Mentor Applications DeveloperCommented:
Not sure you can get the dimensions of an image in memory,
but if you can, you would need to scale the image while maintaining the aspect ratio.

        dim intHeight as integer = Logo.Height
        dim intWidth as integer = Logo.Width

Probably need a class something like following, which can be invoked by:
ImageUtil.Scale(strThumbnailSavePath, 150, 150)

' this would resize the image to a maximum width or height of 150, so whichever is the greater dimension of the image (width or height) would become 150 and the lesser would be sized maintaining aspect ratio.

You could also modify the following class to pass the actual image object ByVal, instead of the path to the image; and then return the scaled image object.
Imports Microsoft.VisualBasic

Public Class ImageUtil

   Public Shared Function ThumbnailCallback() As Boolean
        Return False
    End Function

    Public Shared Sub Scale(ByVal strLogicalPath As String, _
                   Optional ByVal intMaximumHeight As Integer = 430, _
                   Optional ByVal intMaximumWidth As Integer = 430)

        On Error GoTo ReportError

        Dim dummyCallBack As System.Drawing.Image.GetThumbnailImageAbort

        Dim objFullSizeImage As System.Drawing.Image = Nothing
        Dim objScaledImage As System.Drawing.Image = Nothing

        Dim strErrMsg As String = ""

        Dim intFullSizeImageHeight As Integer = 0
        Dim intFullSizeImageWidth As Integer = 0
        Dim intDeltaWidth As Integer = 0
        Dim intDeltaHeight As Integer = 0

        Dim dblScaleFactor As Double = 0

        ' Check the size of the file if too big  then resize it
        objFullSizeImage = System.Drawing.Image.FromFile(strLogicalPath)
        intFullSizeImageHeight = objFullSizeImage.Height
        intFullSizeImageWidth = objFullSizeImage.Width

        ' Set maximum width or height for images
        If intFullSizeImageWidth > intMaximumWidth Or intFullSizeImageHeight > intMaximumHeight Then

            intDeltaWidth = intFullSizeImageWidth - intMaximumWidth
            intDeltaHeight = intFullSizeImageHeight - intMaximumHeight

            'Determine what dimension is off by more
            If intDeltaHeight > intDeltaWidth Then
                'Scale by the height
                dblScaleFactor = intMaximumHeight / intFullSizeImageHeight
                'Scale by the Width
                dblScaleFactor = intMaximumWidth / intFullSizeImageWidth
            End If

            intFullSizeImageWidth *= dblScaleFactor
            intFullSizeImageHeight *= dblScaleFactor

        End If

        ' Fix poor image quality
        ' See Sub Notes()

        objFullSizeImage.Save(strLogicalPath, System.Drawing.Imaging.ImageFormat.Jpeg)

        'Do we need to shrink(resize) this image to max width or height 
        'or is the image already smaller than that?
        If intFullSizeImageHeight > 0 And intFullSizeImageWidth > 0 Then
            dummyCallBack = New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)

            objScaledImage = objFullSizeImage.GetThumbnailImage(intFullSizeImageWidth, intFullSizeImageHeight, dummyCallBack, IntPtr.Zero)

            objScaledImage.Save(strLogicalPath, System.Drawing.Imaging.ImageFormat.Jpeg)

        End If

        On Error Resume Next

        If objScaledImage IsNot Nothing Then
        End If

        If objFullSizeImage IsNot Nothing Then
        End If

        Exit Sub


        strErrMsg = "Error in App_Code Class ImageUtil.Scale()" _
          & vbCr & "Error number " & CStr(Err.Number) _
          & " was generated by " & Err.Source _
          & vbCr & vbCr & Err.Description

        Resume ExitProcedure

    End Sub

End  Class

Open in new window

Anyhow, hope it gets you off in the right direction.

Alan ";0)
arthurh88Author Commented:
that helped, and here is the final code that works like a charm:

 Dim NewHeight As Integer = 0
                Dim NewWidth As Integer = 0
                Dim maxphotowidth As Integer = 450
                Dim maxphotoheight As Integer = 100
                ' Takes a File Stream reference object and converts it to the appropriately sized byte stream
                Dim OldImage As System.Drawing.Image

                Dim ic As New ImageConverter
                Dim OutputStream As New MemoryStream

                ' Create the new image from the Original
                OldImage = System.Drawing.Image.FromStream(stream2)
                'Determine what dimension is off by more
                '400 x 348.   deltawidth = 1.149
                Dim Ratio As Double = MaxPhotoWidth / MaxPhotoHeight
                Dim deltaWidth As Double = OldImage.Width - MaxPhotoWidth
                Dim deltaHeight As Integer = OldImage.Height - MaxPhotoHeight
                Dim scaleFactor As Double
                deltaWidth = OldImage.Width / OldImage.Height

                If (OldImage.Height <= MaxPhotoHeight And OldImage.Width <= MaxPhotoWidth) Then
                    ' Dont stretch a smaller original
                    NewHeight = OldImage.Height
                    NewWidth = OldImage.Width
                    ' Resize the image maintaining the original image aspect ratio.
                    If deltaWidth < Ratio Then
                        'Scale by the height
                        scaleFactor = MaxPhotoHeight / OldImage.Height
                        'Scale by the Width
                        scaleFactor = MaxPhotoWidth / OldImage.Width
                    End If

                    NewHeight = CInt(OldImage.Height * scaleFactor)
                    NewWidth = CInt(OldImage.Width * scaleFactor)

                End If

                'Create the new image
                Dim NewImage As New System.Drawing.Bitmap(OldImage, NewWidth, NewHeight)

                ' Convert the image to a stream ready to save

                graphicImage.DrawImage(NewImage, New Point(20, 300))
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.