1.直接使用接口服务

_pictureService.GetPictureUrl((int)entity.SponsorPictureId); //entity是具体查询出来的实体对象   SponsorPictureId是entity实体中的图片Id

2.GetPictureUrl方法存在IPicture接口的实现类中

(2.1) IPicture 接口的定义

string GetPictureUrl(int pictureId,
int targetSize = ,
bool showDefaultPicture = true,
string storeLocation = null,
PictureType defaultPictureType = PictureType.Entity);

(2.2)接口的实现

 public virtual string GetPictureUrl(int pictureId,
int targetSize = ,
bool showDefaultPicture = true,
string storeLocation = null,
PictureType defaultPictureType = PictureType.Entity)
{
var picture = GetPictureById(pictureId);//通过id得到一个对象
return GetPictureUrl(picture, targetSize, showDefaultPicture, storeLocation, defaultPictureType);
}
(2.2.1)GetPictureById方法
  public virtual Picture GetPictureById(int pictureId)
{
if (pictureId == )
return null; return _pictureRepository.GetById(pictureId);
}

(2.2.2)GetPictureUrl方法

 public virtual string GetPictureUrl(Picture picture,
int targetSize = ,
bool showDefaultPicture = true,
string storeLocation = null,
PictureType defaultPictureType = PictureType.Entity)
{
string url = string.Empty;
byte[] pictureBinary = null;
if (picture != null)
pictureBinary = LoadPictureBinary(picture);
if (picture == null || pictureBinary == null || pictureBinary.Length == )
{
if (showDefaultPicture)
{
url = GetDefaultPictureUrl(targetSize, defaultPictureType, storeLocation);
}
return url;
} if (picture.IsNew)
{
DeletePictureThumbs(picture); //we do not validate picture binary here to ensure that no exception ("Parameter is not valid") will be thrown
picture = UpdatePicture(picture.Id,
pictureBinary,
picture.MimeType,
picture.SeoFilename,
picture.AltAttribute,
picture.TitleAttribute,
false,
false);
} var seoFileName = picture.SeoFilename; // = GetPictureSeName(picture.SeoFilename); //just for sure string lastPart = GetFileExtensionFromMimeType(picture.MimeType);
string thumbFileName;
if (targetSize == )
{
thumbFileName = !String.IsNullOrEmpty(seoFileName)
? string.Format("{0}_{1}.{2}", picture.Id.ToString(""), seoFileName, lastPart)
: string.Format("{0}.{1}", picture.Id.ToString(""), lastPart);
}
else
{
thumbFileName = !String.IsNullOrEmpty(seoFileName)
? string.Format("{0}_{1}_{2}.{3}", picture.Id.ToString(""), seoFileName, targetSize, lastPart)
: string.Format("{0}_{1}.{2}", picture.Id.ToString(""), targetSize, lastPart);
}
string thumbFilePath = GetThumbLocalPath(thumbFileName); //the named mutex helps to avoid creating the same files in different threads,
//and does not decrease performance significantly, because the code is blocked only for the specific file.
using (var mutex = new Mutex(false, thumbFileName))
{
if(!GeneratedThumbExists(thumbFilePath, thumbFileName))
{
mutex.WaitOne(); //check, if the file was created, while we were waiting for the release of the mutex.
if (!GeneratedThumbExists(thumbFilePath, thumbFileName))
{
byte[] pictureBinaryResized; //resizing required
if (targetSize != )
{
using (var stream = new MemoryStream(pictureBinary))
{
Bitmap b = null;
try
{
//try-catch to ensure that picture binary is really OK. Otherwise, we can get "Parameter is not valid" exception if binary is corrupted for some reasons
b = new Bitmap(stream);
}
catch (ArgumentException exc)
{
_logger.Error(string.Format("Error generating picture thumb. ID={0}", picture.Id),
exc);
} if (b == null)
{
//bitmap could not be loaded for some reasons
return url;
} using (var destStream = new MemoryStream())
{
var newSize = CalculateDimensions(b.Size, targetSize);
ImageBuilder.Current.Build(b, destStream, new ResizeSettings
{
Width = newSize.Width,
Height = newSize.Height,
Scale = ScaleMode.Both,
Quality = _mediaSettings.DefaultImageQuality
});
pictureBinaryResized = destStream.ToArray();
b.Dispose();
}
}
}
else
{
//create a copy of pictureBinary
pictureBinaryResized = pictureBinary.ToArray();
} SaveThumb(thumbFilePath, thumbFileName, picture.MimeType, pictureBinaryResized);
} mutex.ReleaseMutex();
} }
url = GetThumbUrl(thumbFileName, storeLocation);
return url;//返回一个路径
}

以上只做为学习参考

 

通过PicturreId获取图片路径(Url)的更多相关文章

  1. PHP用正则批量替换Img中src内容,用正则表达式获取图片路径实现缩略图功能

    PHP用正则批量替换Img中src内容,用正则表达式获取图片路径实现缩略图功能 网上很多正则表达式只能获取或者替换一个img的src内容,或者只能替换固定的字符串,要动态替换多个图片内容的试了几个小时 ...

  2. PHP用正则批量替换Img中src内容,用正则表达式获取图片路径实现缩略图功能

    PHP用正则批量替换Img中src内容,用正则表达式获取图片路径实现缩略图功能 网上很多正则表达式只能获取或者替换一个img的src内容,或者只能替换固定的字符串,要动态替换多个图片内容的试了几个小时 ...

  3. swift获取图片路径出错

    获取图片路径 用以下方式获取 let path = Bundle.main.path(forResource: "ImageName", ofType: "ImageTy ...

  4. ruby -- 进阶学习(十四)设置background-image(解决无法获取图片路径问题)

    基于rails4.0环境 为了美化界面,添加背景图片,于是又傻逼了一回~~ 一开始在xxx.html.erb中添加:(注:图片的路径为:app/asssets/images/background.jp ...

  5. Android 4.4从图库选择图片,获取图片路径并裁剪

    转至 http://blog.csdn.net/tempersitu/article/details/20557383 最近在做一个从图库选择图片或拍照,然后裁剪的功能.本来是没问题的,一直在用 In ...

  6. Images.xcassets不能获取图片路径

    原文地址:http://www.jianshu.com/p/5358f587af38 Images.xcassets在app打包后,以Assets.car文件的形式出现在bundle中.其作用在于: ...

  7. chrome用type=file获取图片路径并转base64字符串

    1 html页面 <div class="col-sm-2" style="height: 200px;margin-top: 14px;"> &l ...

  8. 查询时,如何保存获取相关路径url

    作为新人,总是会有许多小问题不懂,不知道如何解决的. 这不,这次,遇到了路径获取问题. 在Jsp页面中,获取当前路径,在<script></script>中添加  var ur ...

  9. 获取图片地址url的后缀名

    getNameFromLink(url){ if(url.indexOf('.cn/') !== -1){          return (url.split('.')[url.split('.') ...

随机推荐

  1. arcgis api for silverlight开发系列之二:缓存图层与动态图层及图层总结 .

    本文摘自:http://blog.csdn.net/leesmn/article/details/6916458(很优秀的博客)   作为ESRI的平台的一份子arcgis api for silve ...

  2. Netty自定义Encoder/Decoder进行对象传递

    转载:http://blog.csdn.net/top_code/article/details/50901623 在上一篇文章中,我们使用Netty4本身自带的ObjectDecoder,Objec ...

  3. PHP:第五章——字符串与数组及其他函数

    <?php header("Content-Type:text/html;charset=utf-8"); //1.str_split——将字符串转换为数组. /*$str= ...

  4. kmp&扩展kmp

    kmp: KMP的主要目的是求B是不是A的子串,以及若是,B在A中所有出现的位置 写的很详细的大佬的博客:http://www.matrix67.com/blog/archives/115 模板: / ...

  5. ISE创建Microblaze软核(二)

    ISE创建Microblaze软核(二) (2012-07-13 15:09:08) 转载▼ 标签: 杂谈 分类: FPGA开发 第四步 进入Platform Studio操作界面 通过向导创建软核后 ...

  6. js实现trim()方法

    在面向对象编程里面去除字符串左右空格是很容易的事,可以使用trim().ltrim() 或 rtrim(),在jquery里面使用$.trim()也可以轻松的实现.但是在js中却没有这个方法.下面的实 ...

  7. Win2D 中的游戏循环:CanvasAnimatedControl

    Win2D 是 DirectX 的一个高层封装,提供了极大 DirectX 性能的同时,又具有很好用的 API 设计. 用 Win2D 除了能做出高性能的视觉效果之外,还可以轻而易举地搭建一个游戏循环 ...

  8. 【idea】Error:java: Compilation failed: internal java compiler error 解决办法

    报错信息: 报错原因: 项目中Java版本不一致,可以查看项目中的jdk配置 1.查看项目的jdk(Ctrl+Alt+shift+S) File ->Project Structure-> ...

  9. POJ3696 The Luckiest number

    题意 Language:Default The Luckiest number Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7 ...

  10. 洛谷九月月赛II

    题解:模拟 一旦不匹配就要break #include<iostream> #include<cstdio> #include<cstring> #include& ...