<WebMethod()> _
Public Function ProfileUploader() As String
Try
Dim fileName As String = Guid.NewGuid.ToString.ToLower
Dim length As Integer = CInt(Context.Request.InputStream.Length)
Dim buffer As Byte() = New Byte(length - 1) {}
Context.Request.InputStream.Read(buffer, 0, length)
Dim ms As New MemoryStream(buffer)
GenerateThumbnails(1, ms, System.Web.Hosting.HostingEnvironment.MapPath("~/ImageStorage/") & fileName)
'Dim fs As New FileStream(System.Web.Hosting.HostingEnvironment.MapPath("~/ImageStorage/") & fileName, FileMode.Create)
'ms.WriteTo(fs)
ms.Close()
'fs.Close()
'fs.Dispose()
Dim js As New JavaScriptSerializer()
Context.Response.Expires = -1
Context.Response.ContentType = "application/json"
Context.Response.Write(js.Serialize(fileName))
Try
Context.Response.End()
Catch ex1 As Threading.ThreadAbortException
End Try
Catch ex As Exception
Return String.Empty
End Try
End Function
Private Sub GenerateThumbnails(ByVal scaleFactor As Double, ByVal sourcePath As Stream, ByVal targetPath As String)
Dim image = Drawing.Image.FromStream(sourcePath)
Dim newWidth = CType((image.Width * scaleFactor), Integer)
Dim newHeight = CType((image.Height * scaleFactor), Integer)
Dim thumbnailImg = New Drawing.Bitmap(newWidth, newHeight)
Dim thumbGraph = Drawing.Graphics.FromImage(thumbnailImg)
thumbGraph.CompositingQuality = CompositingQuality.HighQuality
thumbGraph.SmoothingMode = SmoothingMode.HighQuality
thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic
Dim imageRectangle = New Drawing.Rectangle(0, 0, newWidth, newHeight)
thumbGraph.DrawImage(image, imageRectangle)
thumbnailImg.Save(targetPath, image.RawFormat)
End Sub