Uploading image files to a web site.

mike55

Contributor
Joined
Mar 26, 2004
Messages
727
Location
Ireland
hi all

I have a FileUpload control on my page, and a button control called btnSave. I also have code that is responsible for uploading the images. The code also resizes and compresses the image.

I have enclosed the code that responds to the btnSave handler in a try catch statement, with a stop statement with in the catch exception. I also have a number of break points within the btnSave handler code. The problem that occurs is that when I try and upload large images i.e. 12.7MB the btnSave handler code does not appear to run, and all I get is the page "Page cannot be display" as if I had no connection to the internet.

Everything runs fine if the image size is low i.e. 108Kb. What I cannot understand is why the button code does not run and then thows an exception if the file size is too large.

Mike55.
 
Found the solution, add the following line of code to the web.config file: <httpRuntime maxRequestLength="102400" />
in the system.web tag area.

Mike55.
 
I am running into a problem with uploading the images into a web. I am using the following code to take an image and create a thumbnail of it:
Code:
 Private Function UploadThumbnail(ByVal myUpload As FileUpload, ByVal nameExtension As String, ByVal myNewHeight As Integer, ByVal myNewWidth As Integer, ByVal compression As Int16, ByVal leadName As String) As Boolean

        Dim myresult As Boolean = True
        'Const bmpW = 300 'New image target width
        Dim bmpW = myNewWidth

        'Const bmpH = 226 'New Image target height 
        Dim bmpH = myNewHeight

        If (myUpload.HasFile) Then
            'Clear the error label text 

            lblError.Text = ""

            'Check to make sure the file to upload has a picture file format extention and set the target width and height

            If (CheckFileType(myUpload.FileName)) Then

                Dim newWidth As Integer = bmpW

                Dim newHeight As Integer = bmpH

                'Use the uploaded filename for saving without the '.' extension 

                Dim upName As String = Mid(myUpload.FileName, 1, (InStr(myUpload.FileName, ".") - 1))

                upName = nameExtension & leadName & lblLandRef.Text

                'Set the save path of the resized image, you will need this directory already created in your web site 
                Dim filePath As String = "" & upName & ".jpg"

                'Create a new Bitmap using the uploaded picture as a Stream 

                'Set the new bitmap resolution to 72 pixels per inch 

                Dim upBmp As Bitmap = Bitmap.FromStream(myUpload.PostedFile.InputStream)

                Dim newBmp As Bitmap = New Bitmap(newWidth, newHeight, Imaging.PixelFormat.Format24bppRgb)

                newBmp.SetResolution(72, 72)

                'Get the uploaded image width and height 

                Dim upWidth As Integer = upBmp.Width

                Dim upHeight As Integer = upBmp.Height

                Dim newX As Integer = 0 'Set the new top left drawing position on the image canvas

                Dim newY As Integer = 0

                Dim reDuce As Decimal

                'Keep the aspect ratio of image the same if not 4:3 and work out the newX and newY positions 

                'to ensure the image is always in the centre of the canvas vertically and horizontally 

                If upWidth > upHeight Then 'Landscape picture 

                    reDuce = newWidth / upWidth

                    'calculate the width percentage reduction as decimal 

                    newHeight = Int(upHeight * reDuce)

                    'reduce the uploaded image height by the reduce amount 

                    newY = Int((bmpH - newHeight) / 2)

                    'Position the image centrally down the canvas 

                    newX = 0 'Picture will be full width 

                ElseIf upWidth < upHeight Then 'Portrait picture 

                    reDuce = newHeight / upHeight

                    'calculate the height percentage reduction as decimal 

                    newWidth = Int(upWidth * reDuce)

                    'reduce the uploaded image height by the reduce amount 

                    newX = Int((bmpW - newWidth) / 2)

                    'Position the image centrally across the canvas 

                    newY = 0 'Picture will be full hieght 

                ElseIf upWidth = upHeight Then 'square picture 

                    reDuce = newHeight / upHeight

                    'calculate the height percentage reduction as decimal 

                    newWidth = Int(upWidth * reDuce)

                    'reduce the uploaded image height by the reduce amount 

                    newX = Int((bmpW - newWidth) / 2) 'Position the image centrally across the canvas 

                    newY = Int((bmpH - newHeight) / 2) 'Position the image centrally down the canvas 

                End If

                'Create a new image from the uploaded picture using the Graphics class 

                'Clear the graphic and set the background colour to white 

                'Use Antialias and High Quality Bicubic to maintain a good quality picture 

                'Save the new bitmap image using 'Png' picture format and the calculated canvas positioning 

                Dim newGraphic As Graphics = Graphics.FromImage(newBmp)

                Try

                    newGraphic.Clear(Color.White)

                    newGraphic.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias

                    newGraphic.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic

                    newGraphic.DrawImage(upBmp, newX, newY, newWidth, newHeight)

                    'Dim ep As EncoderParameters = CompressImageParameters(newBmp)
                    Dim eps As EncoderParameters = New EncoderParameters(1)
                    eps.Param(0) = New EncoderParameter(Encoder.Quality, _
                        compression)
                    Dim ici As ImageCodecInfo = GetEncoderInfo("image/jpeg")

                    'newBmp.Save(MapPath(filePath), ici, eps)
                    newBmp.Save(MapPath(filePath), Imaging.ImageFormat.Jpeg)

                    Return myresult
                Catch ex As Exception

                    myresult = False
                    lblError.Text = ex.ToString

                Finally

                    upBmp.Dispose()

                    newBmp.Dispose()

                    newGraphic.Dispose()

                End Try

            Else

                lblError.Text = "Please select a picture with a file format extension of either Bmp, Jpg, Jpeg, Gif or Png."
                myresult = False
                Return myresult
            End If

        End If

    End Function

and I am also using the following code to simply upload an image:
Code:
Private Function UploadStandardImage() As Boolean
        Dim result As Boolean = True
        Try
            If Me.FileUpload1.HasFile Then
                If CheckFileType(FileUpload1.FileName) = True Then
                    Dim strName As String
                    strName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName)
                    FileUpload1.PostedFile.SaveAs(Server.MapPath(lblLandRef.Text & ".jpg"))
                End If
            End If

            Return result
        Catch ex As Exception
            result = False
        End Try
    End Function

The problem that I am having is that I need to supply a username and password, to allow the upload. How can I supply such data?

Mike55.
 
Back
Top