Example #1
0
        public void MakeThumbnail(string path, int s_width, int s_height, string saveFilePath)
        {
            if (string.IsNullOrEmpty(saveFilePath) || string.IsNullOrEmpty(path))
            {
                return;
            }
            Image img   = Image.FromFile(path);
            Image thumb = MakeThumbnail(img, s_width, s_height);

            img.Dispose();
            var savePath = Path.GetDirectoryName(saveFilePath);
            var saveFile = Path.GetFileName(saveFilePath);

            ImageFileManager.SaveTo(thumb, savePath, saveFile);
            thumb.Dispose();
        }
Example #2
0
        public List <string> UploadImage(List <HttpPostedFileBase> stream, int s_width, int s_height, string saveFilePath, bool isBuildThunb)
        {
            var           savePath = HttpContext.Current.Server.MapPath(saveFilePath); //saveFilePath;
            List <string> paths    = new List <string>();

            if (!string.IsNullOrEmpty(saveFilePath))
            {
                string fileNewName  = string.Empty;
                string fileExt      = string.Empty;
                string fileNameDate = string.Empty;
                foreach (var item in stream)
                {
                    fileNameDate = DateTime.Now.ToString("yyyyMMddHHmmssffffff");
                    Image imgBase = null;
                    Image img     = Image.FromStream(item.InputStream);
                    if (img.Width > 1920)
                    {
                        int imgBaseHeight = Convert.ToInt32(((1920 * 1.0) / img.Width) * img.Height);    //原图 宽度1000
                        imgBase = MakeThumbnail(img, img.Width, imgBaseHeight);
                    }
                    else
                    {
                        imgBase = img;
                    }
                    fileExt     = Path.GetExtension(item.FileName).ToLower();
                    fileNewName = fileNameDate + fileExt;
                    ImageFileManager.SaveTo(imgBase, savePath, fileNewName);
                    paths.Add(saveFilePath + fileNewName);
                    if (isBuildThunb)
                    {
                        int   imgThunbHeight = Convert.ToInt32(((s_width * 1.0) / img.Width) * img.Height);  //缩略图
                        Image thumb          = MakeThumbnail(imgBase, s_width, imgThunbHeight);
                        fileNewName = fileNameDate + fileExt + s_width.ToString() + "x" + fileExt;
                        ImageFileManager.SaveTo(thumb, savePath, fileNewName);
                        thumb.Dispose();
                    }
                    imgBase.Dispose();
                    img.Dispose();
                }
            }
            return(paths);
        }
Example #3
0
        /// <summary>
        /// 返回 保存的缩略文件名 原名!<s_width>x<s_height>
        /// </summary>
        /// <param name="path"></param>
        /// <param name="s_width"></param>
        /// <param name="s_height"></param>
        /// <returns></returns>
        public string MakeThumbnail(string path, int s_width, int s_height)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(string.Empty);
            }
            Image img      = Image.FromFile(path);
            Image thumb    = MakeThumbnail(img, s_width, s_height);
            int   width    = thumb.Width;
            int   height   = thumb.Height;
            var   savePath = Path.GetDirectoryName(path);
            var   saveFile = Path.GetFileNameWithoutExtension(path) + "tmp" + Path.GetExtension(path);

            ImageFileManager.SaveTo(thumb, savePath, saveFile);
            img.Dispose();
            thumb.Dispose();
            var filePath = path + "!" + width.ToString() + "x" + height.ToString(); //DateTime.Now.ToString("yyyyMMddHHmmssffffff") + new Random(DateTime.Now.Millisecond).ToString() + "!" + width.ToString() + "x" + height.ToString();

            File.Move(Path.Combine(savePath, saveFile), filePath);
            return(filePath);
        }