通过PicturreId获取图片路径(Url)
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)的更多相关文章
- PHP用正则批量替换Img中src内容,用正则表达式获取图片路径实现缩略图功能
PHP用正则批量替换Img中src内容,用正则表达式获取图片路径实现缩略图功能 网上很多正则表达式只能获取或者替换一个img的src内容,或者只能替换固定的字符串,要动态替换多个图片内容的试了几个小时 ...
- PHP用正则批量替换Img中src内容,用正则表达式获取图片路径实现缩略图功能
PHP用正则批量替换Img中src内容,用正则表达式获取图片路径实现缩略图功能 网上很多正则表达式只能获取或者替换一个img的src内容,或者只能替换固定的字符串,要动态替换多个图片内容的试了几个小时 ...
- swift获取图片路径出错
获取图片路径 用以下方式获取 let path = Bundle.main.path(forResource: "ImageName", ofType: "ImageTy ...
- ruby -- 进阶学习(十四)设置background-image(解决无法获取图片路径问题)
基于rails4.0环境 为了美化界面,添加背景图片,于是又傻逼了一回~~ 一开始在xxx.html.erb中添加:(注:图片的路径为:app/asssets/images/background.jp ...
- Android 4.4从图库选择图片,获取图片路径并裁剪
转至 http://blog.csdn.net/tempersitu/article/details/20557383 最近在做一个从图库选择图片或拍照,然后裁剪的功能.本来是没问题的,一直在用 In ...
- Images.xcassets不能获取图片路径
原文地址:http://www.jianshu.com/p/5358f587af38 Images.xcassets在app打包后,以Assets.car文件的形式出现在bundle中.其作用在于: ...
- chrome用type=file获取图片路径并转base64字符串
1 html页面 <div class="col-sm-2" style="height: 200px;margin-top: 14px;"> &l ...
- 查询时,如何保存获取相关路径url
作为新人,总是会有许多小问题不懂,不知道如何解决的. 这不,这次,遇到了路径获取问题. 在Jsp页面中,获取当前路径,在<script></script>中添加 var ur ...
- 获取图片地址url的后缀名
getNameFromLink(url){ if(url.indexOf('.cn/') !== -1){ return (url.split('.')[url.split('.') ...
随机推荐
- Vue.js 数据绑定语法详解
Vue.js 数据绑定语法详解 一.总结 一句话总结:Vue.js 的模板是基于 DOM 实现的.这意味着所有的 Vue.js 模板都是可解析的有效的 HTML,且通过一些特殊的特性做了增强.Vue ...
- HDU 2276 矩阵快速幂
Kiki & Little Kiki 2 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- BST树、B树、B+树、B*树
1. BST树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: ...
- 导入arr包
提起项目的aar包 导入目标项目中 添加依赖
- please complete all spokes before continuing 提示
解决方法:输入“1”,按Enter键输入“2”,按Enter键输入“q",按Enter键输入“yes”,按Enter键
- Hosts 文件的作用
问题来源: 我修改了hosts文件访问公司的内网 但是出现错误找不到服务器或DNS错误 一个下午了都上不了公司的系统. Hosts是什么?Hosts是Window系统目录里的一个文件,它的作用可大 ...
- C#统计给定的文本中字符出现的次数,使用循环和递归两种方法
前几天看了一个.net程序员面试题目,题目是”统计给定的文本中字符出现的次数,使用循环和递归两种方法“. 下面是我对这个题目的解法: 1.使用循环: /// <summary> /// 使 ...
- DecimalFormat格式化数字
DecimalFormat格式化数字 DecimalFormat类也是Format的一个子类,主要作用是格式化数字.当然,在格式化数字时要比直接使用NumberFormat更加 方便,因为可以直接指定 ...
- 说说 PADS Layout 中的第 20 层和 第 25层
说说 PADS Layout 中的第 20 层和 第 25层 PADA Layout 有一个不成文的说明,第 20 层和第 25 层各有各的用途. 第 20 层是 Placement Outline ...
- kafka速度为什么快
1. kafka 使用了 分区.分布式.leader/followere 的方式.分布式让 kafka 排除了单点故障,分区和分区复制让数据不丢失2. kafka 使用 zero copy 技术 (基 ...