2
Private Function UploadPic() As String

        Const bmpw As Integer = 300
        Const bmph As Integer = 300
        Dim itemID As String = MaxItemNo()

        Dim filePath As String = "~/Images/FleaMarket/uploadedImages/" & User.Identity.Name.ToString & "/" & itemID & Path.GetExtension(fuImage.FileName)
        MsgBox(filePath)
        If fuImage.HasFile Then
            'lblMessage.Text = ""
            If checkFileType(fuImage.FileName) Then
                **fuImage.PostedFile.SaveAs(filePath)**
                Dim newWidth As Integer = bmpw
                Dim newHeight As Integer = bmph
                Dim upBmp As Bitmap = Bitmap.FromStream(fuImage.PostedFile.InputStream)
                Dim newBmp As Bitmap = New Bitmap(newWidth, newHeight, Imaging.PixelFormat.Format24bppRgb)
                newBmp.SetResolution(72, 72)
                Dim upWidth As Integer = upBmp.Width
                Dim upHeight As Integer = upBmp.Height
                Dim newX As Integer = 0
                Dim newY As Integer = 0
                Dim reduce As Decimal

                If upWidth > upHeight Then
                    reduce = newWidth / upWidth
                    newHeight = Int(upHeight * reduce)
                    newY = Int((bmph - newHeight) / 2)
                    newX = 0
                ElseIf upWidth < upHeight Then
                    reduce = newHeight / upHeight
                    newWidth = Int(upWidth * reduce)
                    newX = Int((bmpw - newWidth) / 2)
                    newY = 0
                ElseIf upWidth = upHeight Then
                    reduce = newHeight / upHeight
                    newWidth = Int(upWidth * reduce)
                    newX = Int((bmpw - newWidth) / 2)
                    newY = Int((bmph - newHeight) / 2)
                End If

                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)
                    newBmp.Save(MapPath(filePath), Imaging.ImageFormat.Png)
                    'Image1.ImageUrl = filePath
                    'Image1.Visible = True
                Catch ex As Exception
                    'lblMessage.Text = ex.ToString
                Finally
                    upBmp.Dispose()
                    newBmp.Dispose()
                    newGraphic.Dispose()
                End Try

            Else
                lblMessage.Text = "Please select bmp, jpg, jpeg, gif or png as file format!"
            End If
        End If
        Return filePath
    End Function

This is the code i am using to upload a pic ....but i get an error " The SaveAs method is configured to require a rooted path, and the path '~/Images/FleaMarket/uploadedImages/mitali2054/5.jpg' is not rooted."

Sify Juhy
  • 177
  • 1
  • 5
  • 20

1 Answers1

3

Server.MapPath should be configured to write disk on serverside:

Basic usage:

String FilePath = Server.MapPath("/App_Data");

To configure: you can take referance these two Q&A

Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"\"), Server.MapPath("/"). What is the difference?

The SaveAs method is configured to require a rooted path, and the path 'fp' is not rooted

Community
  • 1
  • 1
asdf_enel_hak
  • 7,114
  • 3
  • 36
  • 77
  • `fuImage.PostedFile.SaveAs(Server.MapPath("/Images/FleaMarket/uploadedImages/" & User.Identity.Name.ToString & "/" & itemID & Path.GetExtension(fuImage.FileName)))` I am using this code now but it says **" Failed to map the path '/Images/FleaMarket/uploadedImages/mitali2054/5.JPG'."** is it because the folders are not already there??i want them creaed dynamically... – Sify Juhy Oct 23 '11 at 06:09
  • i just have the `Images` folder and want the other folders to be created dynamically...how can i do that?? – Sify Juhy Oct 23 '11 at 06:20