C# 在ImageResizer中解析图像路径

C# 在ImageResizer中解析图像路径,c#,asp.net,string,imageresizer,C#,Asp.net,String,Imageresizer,我正在动态调整图像大小,因此: 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(); 如何获得图像文件名-解析此文件的最佳方法 所需字符串:/eventimage

我正在动态调整图像大小,因此:

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();
如何获得图像文件名-解析此文件的最佳方法


所需字符串:/eventimages/56b640bf5ba43e8aa161fff775c5f97_business.jpg

只需使用正则表达式即可

Regex.Match
创建您的模式并提取所需的值

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("\\", "/");
}

只需使用正则表达式

Regex.Match
创建您的模式并提取所需的值

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("\\", "/");
}
像下面这样

var sitePath = MapPath(@"~");
var relativePath= i.FinalPath.Replace(sitePath, "~");
像下面这样

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

以下是我在实用方法中使用的内容:

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

(从其他人那里偷来的…不记得是谁,但谢谢)

以下是我在实用方法中使用的:

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

(从其他人那里偷来的…不记得是谁,但谢谢)

Server.MapPath抛出:“C:/inetpub/wwwroot/Church/eventimages/c285d6fb5ce44ee09d10ffaf04d80875_guitart.jpg”是一个物理路径,但应该是一个虚拟路径。不知道页面的MapPath方法!您也可以使用ImageResizer.Util.PathUtils.GuessVirtualPath()-但它将返回一个虚拟路径,而不是与应用程序相关的路径。Server.MapPath抛出:“C:/inetpub/wwwroot/Church/eventimages/c285d6fb5ce44ee09d10ffaf04d80875_guitart.jpg”是一个物理路径,但应该是一个虚拟路径。不知道页面的映射路径方法!您也可以使用ImageResizer.Util.PathUtils.GuessVirtualPath()-但它将返回一个虚拟路径,而不是一个与应用程序相关的路径。出于某种原因,stackoverflow移除double\,请使用doublesthrows更改所有single\:解析“^C:[a-Za-z0-9\+[a-Za-z0-9\+[a-Za-z0-9\]+[a-Za-z0-9\+.jpg$”-太多了。)我需要一个优雅的解决方案,允许使用不同的驱动器号出于某种原因,请将所有的单\更改为双箭头:解析“^C:[A-Za-z0-9\+[A-Za-z0-9\]+[A-Za-z0-9\+([A-Za-z0-9\]+[A-Za-z0-9\+.jpg)$”-太多)。我需要一个优雅的解决方案,允许使用不同的驱动器号