Example
In this part of code
Public Sub subAddNewPicture(ByVal FileUploader As FileUpload, ByVal strPictureName AsString, ByVal strImgFolder As String)
Dim strImageFolderPath As String
Dim strImagePath As String
Try
' Construct saving path
strImageFolderPath = Path.Combine(Request.PhysicalApplicationPath, strImgFolder)
strImagePath = Path.Combine(strImageFolderPath, strPictureName)
' Upload image this part is not as fit
FileUploader.PostedFile.SaveAs(strImagePath)
FileUploader.PostedFile.InputStream.Dispose()
FileUploader.Dispose()Catch ex As Exception
'Throw ex
End Try
End Sub
All code here
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) HandlesButton2.Click
Dim tamano_fotoP As Integer
Dim tamano_fotoG As Integer
tamano_fotoP = 235
tamano_fotoG = 649
Try
Dim strGuid As String = ""
Dim strPicExtension As String = ""
Dim strFileName As String = ""
If PhotoUpload.HasFile = True Then
strGuid = fnGuid()
strPicExtension = fnGetPictureExtension(PhotoUpload.FileName)
strFileName = strGuid & strPicExtension
txtfoto.Text = strFileName
If strPicExtension <> "none" Then
subAddNewPicture(PhotoUpload, strFileName, "Imagenes")
Resize_Image(tamano_fotoP, tamano_fotoP, strFileName, "~/Imagenes/","P/")
Resize_Image(tamano_fotoG, tamano_fotoG, strFileName, "~/Imagenes/","G/")
sqldata.Insert()
PhotoUpload.PostedFile.InputStream.Dispose()
PhotoUpload.Dispose()
GC.Collect()
eliminar_foto(strFileName, "")
Else
lblmensaje.Text = "Por favor seleccione una imagen..."
End If
Else
lblmensaje.Text = "Por favor seleccione una imagen..."
End If
lblmensaje.Visible = True
Catch ex As Exception
End Try
End Sub
Protected Sub eliminar_temporales()
Dim file() As FileInfo
Dim i As Integer
Dim txtpath As String
txtpath = Path.Combine(Request.PhysicalApplicationPath, "Imagenes")
If txtpath <> "" Then
Dim dir As New DirectoryInfo(txtpath)
file = dir.GetFiles()
If file.Length > 0 Then
For i = 0 To file.Length - 1
Try
file(i).Delete()
Catch ex As Exception
'Throw ex
End Try
Next
End If
End If
End Sub
Public Sub eliminar_foto(ByVal nombre_foto As String, ByVal folder As String)
Dim strImagePath, strImageFolderPath As String
Try
strImageFolderPath = Path.Combine(Request.PhysicalApplicationPath, "Imagenes"& folder)
strImagePath = Path.Combine(strImageFolderPath, nombre_foto)
System.IO.File.Delete(strImagePath)
Catch ex As Exception
'Throw ex
End Try
End Sub
Public Sub Resize_Image(ByVal intMaxWidth As Short, ByVal intMaxHeigth As Short, ByValfoto As String, ByVal folder As String, ByVal atributo As String)
Dim img As Image
img = System.Drawing.Image.FromFile(MapPath(folder & foto))
Dim oImg As Bitmap = New System.Drawing.Bitmap(img)
'Assign image dimensions
Dim oldWidth As Integer
Dim oldHeight As Integer
Dim newWidth As Integer
Dim newHeight As Integer
Dim ratio As Decimal
oldWidth = oImg.Width
oldHeight = oImg.Height
ratio = oldWidth / oldHeight
newWidth = intMaxWidth
newHeight = newWidth / ratio
'rutina para ver si la foto es mas pequena del tamano a recortar dejandola igual
If oImg.Width < intMaxWidth Then
newWidth = oImg.Width
newHeight = oImg.Height
End If
Dim OutputBitmap As Bitmap = New Bitmap(oImg, CInt(newWidth), CInt(newHeight))
Dim encoderParams As System.Drawing.Imaging.EncoderParameters = NewSystem.Drawing.Imaging.EncoderParameters()
Dim quality As Long
Dim imageFormat = img.RawFormat
Dim myresizer As Graphics
myresizer = Graphics.FromImage(OutputBitmap)
'myresizer.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
'myresizer.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
myresizer.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
myresizer.DrawImage(img, 0, 0, newWidth, newHeight)
'Compress image
Dim comp As Integer
If atributo = "G/" Then
comp = 60
Else
comp = 70
End If
quality = comp ' 0 to 100 - 100 highest quality
Dim encoderParam As System.Drawing.Imaging.EncoderParameter = NewSystem.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality)
encoderParams.Param(0) = encoderParam
Dim arrayICI As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders()
Dim jpegICI As ImageCodecInfo
Dim x As Integer
For x = 0 To arrayICI.Length - 1
If (arrayICI(x).FormatDescription.Equals("JPEG")) Then
jpegICI = arrayICI(x)
Exit For
End If
Next
'Save original image
If Not jpegICI Is Nothing Then
OutputBitmap.Save(Server.MapPath(folder) & atributo & foto, jpegICI, encoderParams)
End If
' clean up
OutputBitmap.Dispose()
End Sub
#Region "Public Functions"
' Adds picture to specified folder
Public Sub subAddNewPicture(ByVal FileUploader As FileUpload, ByVal strPictureName AsString, ByVal strImgFolder As String)
Dim strImageFolderPath As String
Dim strImagePath As String
Try
' Construct saving path
strImageFolderPath = Path.Combine(Request.PhysicalApplicationPath, strImgFolder)
strImagePath = Path.Combine(strImageFolderPath, strPictureName)
' Upload image ACTUALIZADO
FileUploader.PostedFile.SaveAs(strImagePath)
FileUploader.PostedFile.InputStream.Dispose()
FileUploader.Dispose()
Catch ex As Exception
'Throw ex
End Try
End Sub
' Function to generate a GUID using current date and time
Public Function fnGuid() As String
Dim strGuid As String = ""
Try
With Today
strGuid = .Year & .Month & .Day & Now.Hour & Now.Minute & Now.Second
End With
Catch ex As Exception
Throw ex
End Try
' returning the guid generated
Return strGuid
End Function
' Returns true if file name has the extension .jpg, .gif, .jpeg
Public Function fnGetPictureExtension(ByVal strPictureName As String) As String
Try
With strPictureName.ToUpper
If .EndsWith(".JPG") Then
Return ".JPG"
ElseIf .EndsWith(".GIF") Then
Return ".GIF"
ElseIf .EndsWith(".JPEG") Then
Return ".JPEG"
ElseIf .EndsWith(".PNG") Then
Return ".PNG"
ElseIf .EndsWith(".BMP") Then
Return ".BMP"
End If
End With
Catch ex As Exception
Throw ex
End Try
' Else if has no extension so it returns ""
Return "none"
End Function
#End Region