Question

I'm resizing an image dynamically thus:

ImageJob i = new ImageJob(file, "~/eventimages/<guid>_<filename:A-Za-z0-9>.<ext>", 
    new ResizeSettings("width=200&height=133&format=jpg&crop=auto"));
i.Build();

I'm attempting to store the image relative URL in the DB. The i.FinalPath property gives me:

C:\inetpub\wwwroot\Church\eventimages\56b640bff5ba43e8aa161fff775c5f97_scenery.jpg

How can I obtain just the image filename - best way to parse this?

Desired string: /eventimages/56b640bff5ba43e8aa161fff775c5f97_scenery.jpg

Was it helpful?

Solution

something like below,

var sitePath = MapPath(@"~");
var relativePath= i.FinalPath.Replace(sitePath, "~");

OTHER TIPS

Just use Regular expressions

Regex.Match

Create you pattern and extract desired value

string input = "C:\\inetpub\\wwwroot\\Church\\eventimages\\56b640bff5ba43e8aa161fff775c5f97_scenery.jpg";
Match match = Regex.Match(input, @"^C:\\[A-Za-z0-9_]+\\[A-Za-z0-9_]+\\[A-Za-z0-9_]+\\([A-Za-z0-9_]+\\[A-Za-z0-9_]+\.jpg)$", RegexOptions.IgnoreCase);
if (match.Success)
{
    // Finally, we get the Group value and display it.
    string path = match.Groups[1].Value.Replace("\\", "/");
}

Here is what I use in a utility method:

Uri uri1 = new Uri(i.FinalPath);
Uri uri2 = new Uri(HttpContext.Current.Server.MapPath("/"));
Uri relativeUri = uri2.MakeRelativeUri(uri1); 

(stolen from someone else... can't remember who, but thanks)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top