Reputation: 15253
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
Upvotes: 1
Views: 396
Reputation: 434
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)
Upvotes: 0
Reputation: 4730
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("\\", "/");
}
Upvotes: 2
Reputation: 63065
something like below,
var sitePath = MapPath(@"~");
var relativePath= i.FinalPath.Replace(sitePath, "~");
Upvotes: 2