zoukankan      html  css  js  c++  java
  • 通过PicturreId获取图片路径(Url)

    1.直接使用接口服务

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

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

     (2.1) IPicture 接口的定义

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

    (2.2)接口的实现

     public virtual string GetPictureUrl(int pictureId,
                int targetSize = 0,
                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 == 0)
                    return null;
    
                return _pictureRepository.GetById(pictureId);
            }

     (2.2.2)GetPictureUrl方法

     public virtual string GetPictureUrl(Picture picture,
                int targetSize = 0,
                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 == 0)
                {
                    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 == 0)
                {
                    thumbFileName = !String.IsNullOrEmpty(seoFileName)
                        ? string.Format("{0}_{1}.{2}", picture.Id.ToString("0000000"), seoFileName, lastPart)
                        : string.Format("{0}.{1}", picture.Id.ToString("0000000"), lastPart);
                }
                else
                {
                    thumbFileName = !String.IsNullOrEmpty(seoFileName)
                        ? string.Format("{0}_{1}_{2}.{3}", picture.Id.ToString("0000000"), seoFileName, targetSize, lastPart)
                        : string.Format("{0}_{1}.{2}", picture.Id.ToString("0000000"), 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 != 0)
                            {
                                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;//返回一个路径
            }

    以上只做为学习参考

     
  • 相关阅读:
    wxpython 简单例子:显示文本框的窗口显示鼠标位置
    wxpython学习:创建最小的空的wxPython程序
    wxPython学习笔记
    5G PDCCH 协议
    FPGA学习
    CCS 5.5下载地址http://www.dianyuan.com/bbs/1492792.html
    朴素贝叶斯
    决策树最后的存储 检测
    决策树 绘图
    决策树 书上的例题
  • 原文地址:https://www.cnblogs.com/wfaceboss/p/6532511.html
Copyright © 2011-2022 走看看