Solved

Server.MapPath Problem

Posted on 2009-07-01
14
674 Views
Last Modified: 2012-06-27
Hello Experts

I am trying to get a virtual path to a gif file I have in the root folder of my domain.

Using Server.MapPath("./Random.gif") I should get the path d/hosting/member/scafmars/site1/Random.gif.  

Instead I am getting file///d/hosting/member/scafmars/site1/Random.gif when I click Properties on the image on the page.

The file/// is causing the image to not appear on the page.

Can anyone tell me why I am getting the file/// preceding the path?

Thanks

RichW
0
Comment
Question by:RichW
  • 7
  • 2
  • 2
  • +3
14 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24760065
Server.MapPath is getting you the physical path.  Why are you using that to display image, btw?  Why not just pass in "/Random.gif" or "~/Random.gif" to point to the root or URL or web application, respectively.
0
 
LVL 4

Author Comment

by:RichW
ID: 24760096
I'm doing image verification and I need a virtual path to the gif file to create the image on the web page.  

The code works on my home pc using XP pro, but on my domain site I'm getting an empty image control.


0
 
LVL 18

Expert Comment

by:Gary Davis
ID: 24760111
It will work on your local web server since the file// will find the local file. However on a remote web server, it will display the page and attempt to pull the file from your local PC.
Use the mappath to verify the image using the web server's file system (File.Exists()) but use the relative path to the image in the web page.
Gary Davis
0
 
LVL 3

Expert Comment

by:sandip-mishra
ID: 24760232
hi, you have used the right sysntax.... can u please tell me the exact file location and path...

Regards

Sandip Mishra

0
 
LVL 4

Author Comment

by:RichW
ID: 24760280
I hear ya, but I can't use the relative path.  The code I'm using wants a virtual path.  When I use the relative path it doesn't work.
0
 
LVL 4

Author Comment

by:RichW
ID: 24760286
The path is d/hosting/member/scafmars/site1/Random.gif.  The image control on the page shows file///d/hosting/member/scafmars/site1/Random.gif  under properties.
0
 
LVL 15

Expert Comment

by:jinal
ID: 24760367
As you want to display image in Webpage.
If you are using Image control and set its ImageUrl property. then don't use server.mappath for that. Actually ImageUrl should be virtual path not physical path. so use
Image1.ImageUrl = "~/Randon.gif";
If you want physical path then choose
string path  = Server.MapPath("~/Random.gif");
 
 
 
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Author Comment

by:RichW
ID: 24760378
jinal, I have to pass the virtual path into a method of my class in order to create the image dynamically.
0
 
LVL 13

Expert Comment

by:drypz
ID: 24760979
Hi! Try this one.

Server.MapPath(".") + @"\Random.gif"
0
 
LVL 15

Expert Comment

by:jinal
ID: 24761351
Can you please explain detail of functionality ? or put some code .
0
 
LVL 4

Author Comment

by:RichW
ID: 24762114
jinal, the functon creates a new image and places a random text string on it to be used for verification when a user is registering on my site.  It's a simple verification procedure.  It works fine on my home pc.  I am simply trying to find out why Server.MapPath is not finding the image in the root folder where it most definitely is.

drypz, thanks for your suggestions, but it doesn't like that either.

RichW
0
 
LVL 18

Accepted Solution

by:
Gary Davis earned 500 total points
ID: 24762423
Like I said, create the image in the folder within you website or virtual directory using the MapPath to determine its location but in the control to display the image, you have to use the web's location using relative or absolute location (src="Random.gif" or src=http://..../Random.gif).
To test, use the browser to locate the image you created directly.
Gary
0
 
LVL 4

Author Comment

by:RichW
ID: 24763259
Here's the method I'm using.  

The call to this method is Dim salt As String = verify.CreateImage(Server.MapPath("./Random.gif"), 100, 200)

If you pass a relative path instead of using Server.MapPath, the height argument "Identifier Expected".

Server.MapPath works fine on my home PC.  The image on my domain account is there where the path says it is.  I just don't understand why the image is not being displayed when run from my domain.
Public Function CreateImage(ByVal path As String, ByVal height As Integer, ByVal width As Integer) As String
 

        'Creates a Random Gif file of provided width and height
 

        'the string on the gif file is rotated randomly
 

        'returns the random string painted
 

        Dim r As New Random 'to generate a random angle
 

        Dim salt As String = CreateSalt(4) 'generates a random string
 

        Dim bmp As New Bitmap(width, height, PixelFormat.Format24bppRgb) 'creates a 24bit bitmap in memory
 

        Dim g As Graphics = Graphics.FromImage(bmp)
 

        g.TextRenderingHint = TextRenderingHint.AntiAlias 'this will smoothen the Font
 

        g.Clear(Color.Black) 'this clears the background and paints specified color
 

        g.DrawRectangle(Pens.White, 1, 1, width - 3, height - 3)
 

        g.DrawRectangle(Pens.Black, 0, 0, width, height)
 

        Dim mymat As New System.Drawing.Drawing2D.Matrix 'matrix used for rotation        transformation()
 

        Dim i As Integer
 

        For i = 0 To Len(salt) - 1 'we will rotate each literal at a specified angle
 

            mymat.Reset()    ' matrix should be initialized to identity matrix
 

            mymat.RotateAt(r.Next(-30, 0), New PointF(width * (0.12 * i), height * 0.5))
 

            'rotate at any angle b/w -30 and 0
 

            g.Transform = mymat 'apply the transform
 

            g.DrawString(salt.Chars(i), New Font("Comic Sans MS", 10, FontStyle.Italic), SystemBrushes.ActiveCaptionText, width * (0.12 * i), height * 0.5) 'draw the text on our(image)
 

            g.ResetTransform()
 

        Next
 

        bmp.Save(path, ImageFormat.Gif)   'save the gif at specified path and name
 

        g.Dispose() 'clean up
 

        bmp.Dispose() 'ok the mess is over
 

        Return salt 'return the string painted for verification
 

    End Function

Open in new window

0
 
LVL 4

Author Closing Comment

by:RichW
ID: 31599048
Thanks gardavis.  I used Server.MapPath again, but later in my calling function I used image.ImageUrl = "Random.gif" and it worked.  
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c# LinkButton OnClientClick 2 34
Tool Box 2 34
SQL Login 17 38
Message not shown 5 30
AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now