Propably there exists a more "Kentico" way. But this method works for me.
public static string imageUrl(string guid, string filename)
{
string port = null;
if (HttpContext.Current.Request.Url.Port == 80)
{
port = "";
}
else
{
port = ":" + HttpContext.Current.Request.Url.Port.ToString();
}
string result = "//" + HttpUtils.joinUrl(HttpContext.Current.Request.Url.Host + port, "getfile", guid, filename + ".aspx");
string KenticoBaseRoot = System.Web.Configuration.WebConfigurationManager.AppSettings["KenticoBaseRoot"];
if (KenticoBaseRoot != null && KenticoBaseRoot.Trim() != "")
{
result = "//" + HttpUtils.joinUrl(HttpContext.Current.Request.Url.Host + port, KenticoBaseRoot, "getfile", guid, filename + ".aspx");
}
return result;
}