ASP.net upload image resize code

I am using this upload image code for ASP.net. In some cases, it doesn’t downsize the file size much at all and in some cases, it makes the image size larger.

This is ASP.Net code, not ASP classic. I am sure @m_hutley would be glad to see that!!

Any help to improve the codes ability to make images smaller is appreciated. The entire code is here except my upload paths I changed. Thanks

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 members.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

Well, the code is set to use the Width as a shrinking factor; it should reduce the dimensions of the image to X by 450. It also is saving it as a Bitmap, which may have various effects on your final output filesize, which is different from the dimensions.

Yes, aware of that. I don’t what the width of the image to extend 450px.

Yes, aware of that too.

Are there any changes that I can drop in the code or replace that would improve it?

Thanks

Well, if you’re importing a JPG, saving as a JPG would make sense…

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.