Solved

ASP.NET VB, Resize my bitmap in memory?

Posted on 2013-02-05
2
742 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now