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;//返回一个路径
}
以上只做为学习参考