Пример #1
0
        /// <summary>
        /// Get Image Info from File.
        /// </summary>
        /// <param name="localImagePath"></param>
        /// <param name="id"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        public static ImageInfo GetImageInfo(string localImagePath, string id, string tag)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentNullException("id");
            }
            if (string.IsNullOrEmpty(localImagePath))
            {
                throw new ArgumentNullException("localImagePath");
            }
            var source = Image.FromFile(localImagePath);

            var imgInfo = new ImageInfo();

            imgInfo.Id        = id;
            imgInfo.Width     = source.Width;
            imgInfo.Height    = source.Height;
            imgInfo.Extension = ImageUtility.GetImageFormat(source).ToString().ToLower();
            imgInfo.Tag       = tag;

            return(imgInfo);
        }
Пример #2
0
        /// <summary>
        /// Get Image thumb url from Github
        /// if the file not existed I will copy from N2SOURCE/{id}.gif
        /// And make thumb file to N2_{w}x{h}/id.gif and return
        /// 取得縮圖的網址,如果沒有的話我會從 N2SOURCE/{id}.gif  來製作縮圖
        /// 放置 N2_{w}x{h}/id.gif 並且回傳網址
        /// </summary>
        /// <param name="id">圖片編號</param>
        /// <param name="w">寬度 如果0 隨高度換算</param>
        /// <param name="h">高度 如果0 隨寬度換算</param>
        /// <returns></returns>
        public string GetImageThumbFromSource(string id, int w, int h)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentNullException("id");
            }

            RepositoryContent repoContent = GetFileRepositoryContent("N2SOURCE" + "/" + id + ".gif");

            if (repoContent == null)
            {
                return(null);
            }

            if (w == 0 && h == 0)
            {
                return(repoContent.DownloadUrl);
            }

            RepositoryContent repoContentThumb = GetFileRepositoryContent("N2_" + w + "x" + h + "/" + id + ".gif");

            if (repoContentThumb != null)
            {
                return(repoContentThumb.DownloadUrl);
            }

            //Get Image From Source
            var bytes       = Convert.FromBase64String(repoContent.EncodedContent);
            var sourceImage = Image.FromStream(new MemoryStream(bytes));

            var thumbHandler = new ImageUtility();

            Image source2 = null;

            //按照寬度,高度隨意
            if (w > 0 && h == 0)
            {
                source2 = thumbHandler.MakeThumbnail(sourceImage, w, h, "W");
            }


            ////按照高度,寬度隨意
            if (h > 0 && w == 0)
            {
                source2 = thumbHandler.MakeThumbnail(sourceImage, w, h, "H");
            }

            ////強制任性
            if (h > 0 && w > 0)
            {
                source2 = thumbHandler.MakeThumbnail(sourceImage, w, h, "WH");
            }

            var memStream = new MemoryStream();

            source2.Save(memStream, sourceImage.RawFormat);
            UpoloadImage(id, memStream.ToArray(), "upload by N2ImageAgent", "N2_" + w + "x" + h);
            sourceImage.Dispose();
            source2.Dispose();

            return(GetFileRepositoryContent("N2_" + w + "x" + h + "/" + id + ".gif").DownloadUrl);
        }