通过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('.') ...
随机推荐
- 关于keyCode, 键盘代码。 和零散的javascript知识。http://js2.coffee/(转化工具)
这个是coffeescript代码 document.addEventListener 'turbolinks:load', -> document.getElementById(" ...
- erlang 一个高性能web框架 Cowboy 的使用笔记
环境:ubuntu_server 1210 目的:构建web版hello world程序 参考链接:http://roberto-aloi.com/blog/2013/07/13/create-dep ...
- JavaScript---详解scroll
scroll scroll--译为‘滚动’,他是非常常用的属性. 滚动宽高 scrollHeight scrollHeight表示元素的总高度,包括由于溢出而无法展示在网页的不可见部分(不要误解为只有 ...
- C++设计与声明——让接口容易被正确使用
一个简答易错的例子 class Date { public Date(int month,int day,int year); } 一年后使用这个接口的时候,写了Date d(15,10,2015)或 ...
- Visual Studio 后期生成事件命令行
set "str=$(ConfigurationName)" if "%str%"=="Release" (xcopy /y/e $(Tar ...
- JMeter VS LoadRunner
对比项 JMeter LoadRunner 安装 简单,下载解压即可 复杂,安装包大于1GB,安装时间较久 录制/回放模式 支持 支持 测试协议 偏少,但用户可自行拓展 较多,用户不可自行拓展 分布式 ...
- 使用C++实现二叉搜索树的数据结构
需要注意的地方: ①二叉搜索树删除一个指定结点R,若R为叶子结点,则将R的父结点中指向R的指针改为指向nullptr:若R的左右子结点一个为空,一个非空,则将R的父结点中指向R的指针改为指向R的非空子 ...
- LARC Caffe笔记(一) Setup
采用杂记的形式 从查找到知乎页面开始:https://www.zhihu.com/question/27982282 到mnist example页面:http://caffe.berkeleyvis ...
- 取出表A中第31到第40记录
方法一: select top 10 * from A where RowId not in (select top 10 RowId from A) 方法二(使用临时表): with tempTab ...
- 《DSP using MATLAB》 示例 Example 9.12
代码: %% ------------------------------------------------------------------------ %% Output Info about ...