Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Resizing image does not give same file size
Message
De
22/01/2015 16:59:59
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Titre:
Resizing image does not give same file size
Versions des environnements
Environment:
VB 9.0
OS:
Windows 8.1
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01614138
Message ID:
01614138
Vues:
43
I have a class which I am using to resize a JPG. Apparently, this technique does not give the same file size depending on which environment it sits on. When I execute this on my Windows 8.1, I do not get the same file size as when this is being executed on Windows Server 2008.
    ' Resize an image
    Public Function ImageResize() As Boolean
        Dim lcFile As String = ""
        Dim lnHeightRatio As Integer = 0
        Dim lnRatio As Double = 0
        Dim lnRatioImage As Double = 0
        Dim lnWidthRatio As Integer = 0
        Dim loFinalImage As Bitmap = Nothing
        Dim loGraphics As System.Drawing.Graphics = Nothing
        Dim loImage As System.Drawing.Bitmap = Nothing
        Dim loImage2 As System.Drawing.Bitmap = Nothing

        ' Reset the values
        lImageHasBeenPreserved = False
        oImage = Nothing

        ' Initialization
        lcFile = Trim(cFile)
        lVerifyForWidthAndHeight = True

        ' If the file does not exist
        If Not oApp.FileExist(lcFile) Then
            cMessage = cFileDoesNotExist
            Return False
        End If

        ' Try to negotiate with the image
        Try
            loImage = New System.Drawing.Bitmap(lcFile)
        Catch loError As Exception
            cMessage = oApp.StrTran(cThisFileIsNotSupported, "##File##", lcFile)
            Return False
        End Try

        ' If we can resize with no crop
        If lResizeButNoCrop Then

            ' Calculate the proper ratio as per the width
            lnHeightRatio = nWidth * loImage.Height / loImage.Width

            ' If the height is greater than the targeted resize height
            If lnHeightRatio > nHeight Then

                ' Adjust the width in regards to that
                nWidth = nHeight * loImage.Width / loImage.Height

                ' Use this flag to bypass the validation when the width and height is defined
                lVerifyForWidthAndHeight = False

            Else

                ' Calculate the proper ratio as per the height
                lnWidthRatio = nHeight * loImage.Width / loImage.Height

                ' If the height is greater than the targeted resize height
                If lnWidthRatio > nWidth Then

                    ' Adjust the height in regards to that
                    nHeight = nWidth * loImage.Height / loImage.Width

                    ' Use this flag to bypass the validation when the width and height is defined
                    lVerifyForWidthAndHeight = False

                End If

            End If

        End If

        ' If the image is already with the proper dimensions
        If nWidth = loImage.Width And nHeight = loImage.Height Then
            lImageHasBeenPreserved = True

            ' This is necessary as the image would remain locked
            loImage.Dispose()

            Return True
        End If

        ' If the width calculated is bigger than the original width or if the height calculated if bigger than the original width
        ' This is in the case the original image is 250x250 and that we would want to resize it to 600x900
        ' In such situation, we preserve the original image otherwise it would be deformed
        If nWidth > loImage.Width Or nHeight > loImage.Height Then
            nWidth = loImage.Width
            nHeight = loImage.Height
        End If

        ' Initialize a new image object on the required dimensions
        loImage2 = New System.Drawing.Bitmap(nWidth, nHeight, loImage.PixelFormat)

        ' If we can resize
        If loImage2.PixelFormat = Drawing.Imaging.PixelFormat.Format1bppIndexed Or _
            loImage2.PixelFormat = Drawing.Imaging.PixelFormat.Format4bppIndexed Or _
            loImage2.PixelFormat = Drawing.Imaging.PixelFormat.Format8bppIndexed Or _
            loImage2.PixelFormat = Drawing.Imaging.PixelFormat.Undefined Or _
            loImage2.PixelFormat = Drawing.Imaging.PixelFormat.DontCare Or _
            loImage2.PixelFormat = Drawing.Imaging.PixelFormat.Format16bppArgb1555 Or _
            loImage2.PixelFormat = Drawing.Imaging.PixelFormat.Format16bppGrayScale Then
            cMessage = cPixelFormatNotSupported

            ' This is necessary as the image would remain locked
            loImage.Dispose()

            Return False
        End If

        ' Initialization
        lnRatio = nWidth / nHeight
        lnRatioImage = loImage.Width / loImage.Height

        ' If the ratio of the image is smaller than the calculcated ratio
        If lnRatioImage < lnRatio Then
            nCropWidth = loImage.Width
            nCropHeight = loImage.Width / lnRatio
        Else
            nCropWidth = loImage.Height * lnRatio
            nCropHeight = loImage.Height
        End If

        ' Crop needs it
        oImage = loImage

        ' If we cannot crop
        If Not Crop() Then

            ' This is necessary as the image would remain locked
            loImage.Dispose()

            Return False
        End If

        ' Create the placeholder
        loFinalImage = New Bitmap(nWidth, nHeight)

        ' Assign the placeholder into the graphic converter
        loGraphics = System.Drawing.Graphics.FromImage(loFinalImage)

        ' Set the parameters
        loGraphics.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
        loGraphics.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
        loGraphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality
        loGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality

        ' Resize the image
        loGraphics.DrawImage(loImage, 0, 0, nWidth, nHeight)

        ' Release the image
        loGraphics.Dispose()

        ' The oImage property can now be assigned to the resized version
        oImage = loFinalImage

        ' If the image has not been altered by the Image class
        If lImageHasBeenPreserved Then
        Else

            ' If we cannot save the image
            If Not SaveAsHighQualityJpegFile() Then

            End If

        End If

        ' Remove this object from memory
        loImage.Dispose()

        ' Reset the values
        cFile = ""
        cFileDestination = ""
        lResizeButNoCrop = False

        Return True
    End Function

    ' Save an image into a JPG format
    Public Function SaveAsHighQualityJpegFile() As Boolean
        Dim loEncoderParameters As System.Drawing.Imaging.EncoderParameters = Nothing
        Dim loImageCodecInfo As System.Drawing.Imaging.ImageCodecInfo = Nothing

        ' Set Image codec of JPEG type, the index of JPEG codec is "1"       
        loImageCodecInfo = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()(1)

        ' Set the parameters for defining the quality of the thumbnail... here it is set to 100%
        loEncoderParameters = New System.Drawing.Imaging.EncoderParameters(1)

        loEncoderParameters.Param(0) = New System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 95)

        oImage.Save(cFileDestination, loImageCodecInfo, loEncoderParameters)

        Return True
    End Function
Anyone can confirm in there something that would be related to OS specific?
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform