ASP.Net Uploader - improve compression and downsizing

35 views Asked by At

I use Classic ASP, not ASP.Net but I do have an ASP.net uploader that does a pretty good job. With some large files it compresses the image and makes it a good workable size. But there are times where the image actual is larger than the original.

I would really appreciate it if one of your experts can tweak my code and make it more efficient. Not knowing Net, I am at a loss.

Any help would be greatly appreciated.

Thank you.

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim strFileName As String = String.Format("Photo{0}.jpg", hdnAgentID.value)
        If fupPhoto.HasFile Then
            Try
                If fupPhoto.PostedFile.ContentLength <= 10000000 Then
                    fupPhoto.SaveAs("M:\my path\" & strFileName)
                    resizeImage("M:\my path & strFileName)
                End If
            Catch ex As Exception
                lblAlert.Text = "ERROR: " & ex.ToString()
            End Try
            hdnPhotoURL.Value = String.Format(https://websiteaddress/uploads/{0}, strFileName)
        End If
        
        strFileName = String.Format("Logo{0}.jpg", hdnAgentID.Value)
        If fupLogo.HasFile Then
            Try
                If fupLogo.PostedFile.ContentLength <= 10000000 Then
                    fupLogo.SaveAs("M:\my path\" & strFileName)
                   resizeImage("M:\my path\" & strFileName)
                End If
    
            Catch ex As Exception
                lblAlert.Text = "ERROR: " & ex.ToString()
            End Try
            hdnLogoURL.Value = String.Format(https://websiteaddress/uploads/{0}, strFileName)
        End If

        
        'Update UI and Create JS to populate the fields on memberpage.asp
      pnlMain.Visible = true
      litJS.visible = true
    End Sub
    

    Private Sub resizeImage(ByVal location As String)
             ' Get the source bitmap.
             Dim bm_source As Bitmap = system.drawing.image.fromfile(location)
        
            If bm_source.Width > 450 Then
                Dim PercentageScaleWidth As Decimal = 450 / bm_source.Width
            
                ' Make a bitmap for the result.
                Dim bm_dest As New Bitmap( _
                    CInt(bm_source.Width * PercentageScaleWidth), _
                    CInt(bm_source.Height * PercentageScaleWidth))

                ' Make a Graphics object for the result Bitmap.
                Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

                ' Copy the source image into the destination bitmap.
                gr_dest.DrawImage(bm_source, 0, 0, _
                    bm_dest.Width + 1, _
                    bm_dest.Height + 1)

                ' Save the result.
                bm_source.Dispose()
                bm_source = Nothing

                Threading.Thread.Sleep(1000)
            
                bm_dest.Save(location)
                bm_dest.Dispose()
                bm_dest = Nothing
    
                gr_dest.Dispose()
                gr_dest = Nothing
            else
            
                bm_source.Dispose()
                bm_source = Nothing
            
            End If
              

    End Sub
1

There are 1 answers

1
AspClassic-Guy On

I have had some jpg's resized from 375 kb to 660 kb. Seems odd that it would be larger which is why I was hoping that altering the code would cure that. Thanks for responding.