Solved

ASP.NET VB, Resize my bitmap in memory?

Posted on 2013-02-05
2
784 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

628 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