I figured out all of the problems. The complete code for resizing and saving is as follows:
'Check to make sure that the uploading file has a value
If Not (CType(e.Item.FindControl("inpFileUp"), HtmlInputFile).PostedFile Is Nothing) Then
'Try
'Make sure path has trailing slash
If Right(strSavePath, 1) <> "\" Then strSavePath = strSavePath & "\"
'Save some information from the upload and set up paths
Dim contentType As String = CType(e.Item.FindControl("inpFileUp"), HtmlInputFile).PostedFile.ContentType
Dim contentLength As Integer = CType(e.Item.FindControl("inpFileUp"), HtmlInputFile).PostedFile.ContentLength
Dim strUploadFileName As String = strImageLocation & strFileEnding
Dim strThisFullFileName As String = strSavePath & strUploadFileName
Dim strTempFullFileName As String = strSavePath & "temp_" & strUploadFileName 'Temp file name/location
lblMessage.Text = "" 'initialize Page Message
'Only allow JPG or GIF files
If (contentType = "image/pjpeg") Or (contentType = "image/jpeg") Or (contentType = "image/gif") Then
CType(e.Item.FindControl("inpFileUp"), HtmlInputFile).PostedFile.SaveAs(strTempFullFileName)
lblMessage.Text = lblMessage.Text & "Accepted Upload: " & strTempFullFileName & "
" & _
"Content Type: " & contentType & "
" & _
"File Size: " & contentLength & "
"
'Resize Image if needed
Dim ThisImage As System.Drawing.Image = System.Drawing.Image.FromFile(strTempFullFileName)
Dim LgHorRes As Long = ThisImage.HorizontalResolution
Dim LgVerRes As Long = ThisImage.VerticalResolution
lblMessage.Text = lblMessage.Text & "Dimensions: " & ThisImage.Width & "(" & LgHorRes & ") x " & ThisImage.Height & "(" & LgVerRes & ")
"
'Determine image orientation
If ThisImage.Width > ThisImage.Height Then
strImageOrientation = "Landscape"
ElseIf ThisImage.Width = ThisImage.Height Then
strImageOrientation = "Square"
Else
strImageOrientation = "Portrait"
End If
'If we have maxWidth or MaxHeight turned on (not set to zero) and the uploaded image is wider or taller, then resize
Dim NewWidth As Long 'Holds Resized width
Dim NewHeight As Long ' Holds Resized height
Dim NewWidth2 As Long 'Holds Resized width #2
Dim NewHeight2 As Long ' Holds Resized height #2
'Too Wide?
If (intMaxWidth > 0) And (ThisImage.Width > intMaxWidth) Then
'Calculate New Width and Height
NewWidth = intMaxWidth
NewHeight = CInt(CInt(ThisImage.Height) / (CInt(ThisImage.Width) / NewWidth))
LgHorRes = ThisImage.HorizontalResolution
LgVerRes = ThisImage.VerticalResolution
lblMessage.Text = lblMessage.Text & "Image will be sized to: " & NewWidth & "(" & LgHorRes & ") x " & NewHeight & "(" & LgVerRes & ")
"
Else
'No resizing required, so just assign Width and Height to what it is now
NewWidth = ThisImage.Width
NewHeight = ThisImage.Height
End If
'Too Tall?
If (intMaxHeight > 0) And (NewHeight > intMaxHeight) Then
'Calculate New Width and Height
NewHeight2 = intMaxHeight
NewWidth2 = CInt(CInt(NewWidth) / (CInt(NewHeight) / NewHeight2))
lblMessage.Text = lblMessage.Text & "Image will be sized to: " & NewWidth2 & " x " & NewHeight2 & "
"
Else
'No resizing required, so just assign Width and Height to what it is now
NewWidth2 = NewWidth
NewHeight2 = NewHeight
End If
'Create new image which is now sized properly
Dim mg As System.Drawing.Image = System.Drawing.Image.FromFile(strTempFullFileName)
Dim newSize As System.Drawing.Size = New Size(NewWidth2, NewHeight2)
Dim bp As Bitmap = New Bitmap(newSize.Width, newSize.Height)
Dim g As Graphics = Graphics.FromImage(bp)
g.SmoothingMode = SmoothingMode.HighQuality
g.InterpolationMode = InterpolationMode.HighQualityBicubic
g.PixelOffsetMode = PixelOffsetMode.HighQuality
Dim rect As Rectangle = New Rectangle(0, 0, newSize.Width, newSize.Height)
g.DrawImage(mg, rect, 0, 0, mg.Width, mg.Height, GraphicsUnit.Pixel)
Dim pItem As PropertyItem
For Each pItem In ThisImage.PropertyItems
bp.SetPropertyItem(pItem)
Next
'Save gif
If (contentType = "image/gif") Then
bp.Save(strThisFullFileName, System.Drawing.Imaging.ImageFormat.Gif)
'Save JPEG
ElseIf (contentType = "image/pjpeg") Or (contentType = "image/jpeg") Then
Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
Dim codec As ImageCodecInfo
Dim i As Integer = 0
While i < codecs.Length
If (codecs(i).MimeType.Equals("image/jpeg")) Then
codec = codecs(i)
End If
i = i + 1
End While
Dim encoderInstance As System.Drawing.Imaging.Encoder
Dim encoderParametersInstance As EncoderParameters
Dim encoderParameterInstance As EncoderParameter
'If codec Is "" Then
encoderInstance = System.Drawing.Imaging.Encoder.Quality
encoderParametersInstance = New EncoderParameters(2)
encoderParameterInstance = New EncoderParameter(encoderInstance, 85L)
encoderParametersInstance.Param(0) = encoderParameterInstance
encoderInstance = System.Drawing.Imaging.Encoder.ColorDepth
encoderParameterInstance = New EncoderParameter(encoderInstance, 24L)
encoderParametersInstance.Param(1) = encoderParameterInstance
'End If
bp.Save(strThisFullFileName, codec, encoderParametersInstance)
End If
lblMessage.Text = lblMessage.Text & "Final File Saved As: " & strThisFullFileName & "
"
jtwright
Member
200 Points
40 Posts
Re: Trouble Resising JPEG
Jan 30, 2004 03:52 AM|LINK
'Check to make sure that the uploading file has a value If Not (CType(e.Item.FindControl("inpFileUp"), HtmlInputFile).PostedFile Is Nothing) Then 'Try 'Make sure path has trailing slash If Right(strSavePath, 1) <> "\" Then strSavePath = strSavePath & "\" 'Save some information from the upload and set up paths Dim contentType As String = CType(e.Item.FindControl("inpFileUp"), HtmlInputFile).PostedFile.ContentType Dim contentLength As Integer = CType(e.Item.FindControl("inpFileUp"), HtmlInputFile).PostedFile.ContentLength Dim strUploadFileName As String = strImageLocation & strFileEnding Dim strThisFullFileName As String = strSavePath & strUploadFileName Dim strTempFullFileName As String = strSavePath & "temp_" & strUploadFileName 'Temp file name/location lblMessage.Text = "" 'initialize Page Message 'Only allow JPG or GIF files If (contentType = "image/pjpeg") Or (contentType = "image/jpeg") Or (contentType = "image/gif") Then CType(e.Item.FindControl("inpFileUp"), HtmlInputFile).PostedFile.SaveAs(strTempFullFileName) lblMessage.Text = lblMessage.Text & "Accepted Upload: " & strTempFullFileName & " " & _ "Content Type: " & contentType & " " & _ "File Size: " & contentLength & " " 'Resize Image if needed Dim ThisImage As System.Drawing.Image = System.Drawing.Image.FromFile(strTempFullFileName) Dim LgHorRes As Long = ThisImage.HorizontalResolution Dim LgVerRes As Long = ThisImage.VerticalResolution lblMessage.Text = lblMessage.Text & "Dimensions: " & ThisImage.Width & "(" & LgHorRes & ") x " & ThisImage.Height & "(" & LgVerRes & ") " 'Determine image orientation If ThisImage.Width > ThisImage.Height Then strImageOrientation = "Landscape" ElseIf ThisImage.Width = ThisImage.Height Then strImageOrientation = "Square" Else strImageOrientation = "Portrait" End If 'If we have maxWidth or MaxHeight turned on (not set to zero) and the uploaded image is wider or taller, then resize Dim NewWidth As Long 'Holds Resized width Dim NewHeight As Long ' Holds Resized height Dim NewWidth2 As Long 'Holds Resized width #2 Dim NewHeight2 As Long ' Holds Resized height #2 'Too Wide? If (intMaxWidth > 0) And (ThisImage.Width > intMaxWidth) Then 'Calculate New Width and Height NewWidth = intMaxWidth NewHeight = CInt(CInt(ThisImage.Height) / (CInt(ThisImage.Width) / NewWidth)) LgHorRes = ThisImage.HorizontalResolution LgVerRes = ThisImage.VerticalResolution lblMessage.Text = lblMessage.Text & "Image will be sized to: " & NewWidth & "(" & LgHorRes & ") x " & NewHeight & "(" & LgVerRes & ") " Else 'No resizing required, so just assign Width and Height to what it is now NewWidth = ThisImage.Width NewHeight = ThisImage.Height End If 'Too Tall? If (intMaxHeight > 0) And (NewHeight > intMaxHeight) Then 'Calculate New Width and Height NewHeight2 = intMaxHeight NewWidth2 = CInt(CInt(NewWidth) / (CInt(NewHeight) / NewHeight2)) lblMessage.Text = lblMessage.Text & "Image will be sized to: " & NewWidth2 & " x " & NewHeight2 & " " Else 'No resizing required, so just assign Width and Height to what it is now NewWidth2 = NewWidth NewHeight2 = NewHeight End If 'Create new image which is now sized properly Dim mg As System.Drawing.Image = System.Drawing.Image.FromFile(strTempFullFileName) Dim newSize As System.Drawing.Size = New Size(NewWidth2, NewHeight2) Dim bp As Bitmap = New Bitmap(newSize.Width, newSize.Height) Dim g As Graphics = Graphics.FromImage(bp) g.SmoothingMode = SmoothingMode.HighQuality g.InterpolationMode = InterpolationMode.HighQualityBicubic g.PixelOffsetMode = PixelOffsetMode.HighQuality Dim rect As Rectangle = New Rectangle(0, 0, newSize.Width, newSize.Height) g.DrawImage(mg, rect, 0, 0, mg.Width, mg.Height, GraphicsUnit.Pixel) Dim pItem As PropertyItem For Each pItem In ThisImage.PropertyItems bp.SetPropertyItem(pItem) Next 'Save gif If (contentType = "image/gif") Then bp.Save(strThisFullFileName, System.Drawing.Imaging.ImageFormat.Gif) 'Save JPEG ElseIf (contentType = "image/pjpeg") Or (contentType = "image/jpeg") Then Dim codecs As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders() Dim codec As ImageCodecInfo Dim i As Integer = 0 While i < codecs.Length If (codecs(i).MimeType.Equals("image/jpeg")) Then codec = codecs(i) End If i = i + 1 End While Dim encoderInstance As System.Drawing.Imaging.Encoder Dim encoderParametersInstance As EncoderParameters Dim encoderParameterInstance As EncoderParameter 'If codec Is "" Then encoderInstance = System.Drawing.Imaging.Encoder.Quality encoderParametersInstance = New EncoderParameters(2) encoderParameterInstance = New EncoderParameter(encoderInstance, 85L) encoderParametersInstance.Param(0) = encoderParameterInstance encoderInstance = System.Drawing.Imaging.Encoder.ColorDepth encoderParameterInstance = New EncoderParameter(encoderInstance, 24L) encoderParametersInstance.Param(1) = encoderParameterInstance 'End If bp.Save(strThisFullFileName, codec, encoderParametersInstance) End If lblMessage.Text = lblMessage.Text & "Final File Saved As: " & strThisFullFileName & " "