Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ASP.NET VB, Resize my bitmap in memory?

Posted on 2013-02-05
2
Medium Priority
?
802 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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

704 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