Solved

ASP.NET VB, Resize my bitmap in memory?

Posted on 2013-02-05
2
765 Views
Last Modified: 2013-02-05
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)
0
Comment
Question by:arthurh88
2 Comments
 
LVL 26

Accepted Solution

by:
Alan Warren earned 500 total points
ID: 38858019
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
            Else
                'Scale by the Width
                dblScaleFactor = intMaximumWidth / intFullSizeImageWidth
            End If

            intFullSizeImageWidth *= dblScaleFactor
            intFullSizeImageHeight *= dblScaleFactor

        End If

        ' Fix poor image quality
        ' See Sub Notes()
        objFullSizeImage.RotateFlip(Drawing.RotateFlipType.Rotate180FlipNone)
        objFullSizeImage.RotateFlip(Drawing.RotateFlipType.Rotate180FlipNone)

        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

ExitProcedure:
        On Error Resume Next

        If objScaledImage IsNot Nothing Then
            objScaledImage.Dispose()
        End If

        If objFullSizeImage IsNot Nothing Then
            objFullSizeImage.Dispose()
        End If


        Exit Sub

ReportError:

        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)
0
 

Author Comment

by:arthurh88
ID: 38858253
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
                Else
                    ' Resize the image maintaining the original image aspect ratio.
                    If deltaWidth < Ratio Then
                        'Scale by the height
                        scaleFactor = MaxPhotoHeight / OldImage.Height
                    Else
                        '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
                OldImage.Dispose()

                graphicImage.DrawImage(NewImage, New Point(20, 300))
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question