Exemplo n.º 1
0
        /// <summary>
        /// 获取缩略图尺寸
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="mode"></param>
        /// <param name="src"></param>
        /// <returns></returns>
        public static ThumbSize getTargetSize(int width, int height, SaveThumbnailMode mode, Image src)
        {
            ThumbSize t = new ThumbSize();

            t.Src   = new Size(src.Width, src.Height);
            t.Point = new Point(0, 0);

            if (mode == SaveThumbnailMode.ByWidth)
            {
                int newHeight = src.Height * width / src.Width;
                t.New = new Size(width, newHeight);
                return(t);
            }
            else if (mode == SaveThumbnailMode.ByHeight)
            {
                int newWidth = src.Width * height / src.Height;
                t.New = new Size(newWidth, height);
                return(t);
            }
            else if (mode == SaveThumbnailMode.ByWidthHeight)
            {
                t.New = new Size(width, height);
                return(t);
            }
            else if (mode == SaveThumbnailMode.Cut)
            {
                return(getCutSize(width, height, mode, src));
            }

            return(getAutoSize(width, height, mode, src));
        }
Exemplo n.º 2
0
        private static Boolean saveThumbSmall(String filename, ThumbnailType ttype)
        {
            int x = 0;
            int y = 0;
            SaveThumbnailMode sm = SaveThumbnailMode.Auto;

            if (ttype == ThumbnailType.Small)
            {
                x  = config.Instance.Site.PhotoThumbWidth;
                y  = config.Instance.Site.PhotoThumbHeight;
                sm = config.Instance.Site.GetPhotoThumbMode();
            }
            else if (ttype == ThumbnailType.Medium)
            {
                x = config.Instance.Site.PhotoThumbWidthMedium;
                y = config.Instance.Site.PhotoThumbHeightMedium;
            }
            else if (ttype == ThumbnailType.Big)
            {
                x = config.Instance.Site.PhotoThumbWidthBig;
                y = config.Instance.Site.PhotoThumbHeightBig;
            }

            try {
                saveThumbImagePrivate(filename, ttype, x, y, sm);
                return(true);
            }
            catch (OutOfMemoryException ex) {
                logger.Error("file format error: " + filename);
                return(false);
            }
        }
Exemplo n.º 3
0
        private static void saveThumbSmall(String filename, ThumbnailType ttype)
        {
            int x = 0;
            int y = 0;
            SaveThumbnailMode sm = SaveThumbnailMode.Auto;

            if (ttype == ThumbnailType.Small)
            {
                x  = config.Instance.Site.PhotoThumbWidth;
                y  = config.Instance.Site.PhotoThumbHeight;
                sm = SaveThumbnailMode.Cut;
            }
            else if (ttype == ThumbnailType.Medium)
            {
                x = config.Instance.Site.PhotoThumbWidthMedium;
                y = config.Instance.Site.PhotoThumbHeightMedium;
            }
            else if (ttype == ThumbnailType.Big)
            {
                x = config.Instance.Site.PhotoThumbWidthBig;
                y = config.Instance.Site.PhotoThumbHeightBig;
            }


            Image img = Image.FromFile(filename);

            if (img.Size.Width <= x && img.Size.Height <= y)
            {
                File.Copy(filename, Img.GetThumbPath(filename, ttype));
            }
            else
            {
                Img.SaveThumbnail(filename, Img.GetThumbPath(filename, ttype), x, y, sm);
            }
        }
Exemplo n.º 4
0
        private static ThumbSize getCutSize(int width, int height, SaveThumbnailMode mode, Image src)
        {
            ThumbSize t = new ThumbSize();

            if ((double)src.Width / (double)src.Height > (double)width / (double)height)
            {
                int newWidth = src.Height * width / height;
                t.Src   = new Size(newWidth, src.Height);
                t.New   = new Size(width, height);
                t.Point = new Point((src.Width - newWidth) / 2, 0);
            }
            else
            {
                int newHeight = src.Width * height / width;
                t.Src   = new Size(src.Width, newHeight);
                t.New   = new Size(width, height);
                t.Point = new Point(0, (src.Height - newHeight) / 2);
            }

            return(t);
        }
Exemplo n.º 5
0
        private static ThumbSize getAutoSize(int width, int height, SaveThumbnailMode mode, Image src)
        {
            ThumbSize t = new ThumbSize();

            t.Src   = new Size(src.Width, src.Height);
            t.Point = new Point(0, 0);

            int newWidth  = width;
            int newHeight = height;

            if ((double)src.Width / (double)src.Height > (double)width / (double)height)
            {
                newHeight = src.Height * width / src.Width;
            }
            else
            {
                newWidth = src.Width * height / src.Height;
            }
            t.New = new Size(newWidth, newHeight);

            return(t);
        }
Exemplo n.º 6
0
 private static void saveThumbImagePrivate( String filename, ThumbnailType ttype, int x, int y, SaveThumbnailMode sm )
 {
     using (Image img = Image.FromFile( filename )) {
         if (img.Size.Width <= x && img.Size.Height <= y) {
             File.Copy( filename, Img.GetThumbPath( filename, ttype ) );
         }
         else {
             Img.SaveThumbnail( filename, Img.GetThumbPath( filename, ttype ), x, y, sm );
         }
     }
 }
Exemplo n.º 7
0
        /// <summary>
        /// 上传图片(自定义保存路径),同时生成最小的缩略图
        /// </summary>
        /// <param name="uploadPath">保存路径(相对路径)</param>
        /// <param name="postedFile">HttpFile</param>
        /// <param name="picName">图片名称</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <returns></returns>
        public static Result SaveImg( String uploadPath, HttpFile postedFile, String picName, int width, int height, SaveThumbnailMode mode )
        {
            logger.Info( "uploadPath : " + uploadPath );
            logger.Info( "picName : " + picName );
            Result result = new Result();

            CheckUploadPic( postedFile, result );
            if (result.HasErrors) return result;

            String str = PathHelper.Map( uploadPath );
            String str2 = picName + "." + Img.GetImageExt( postedFile.ContentType );
            String filename = Path.Combine( str, str2 );
            try {

                String oldFile = null;
                if (file.Exists( filename )) {
                    oldFile = filename + "." + Guid.NewGuid() + Path.GetExtension( filename );
                    file.Move( filename, oldFile );
                }

                postedFile.SaveAs( filename );

                try {
                    saveThumbImagePrivate( filename, ThumbnailType.Small, width, height, mode );

                    if (strUtil.HasText( oldFile )) {
                        file.Delete( oldFile );
                    }
                }
                catch (OutOfMemoryException ex) {

                    file.Delete( filename );
                    if (strUtil.HasText( oldFile )) {
                        file.Move( oldFile, filename );
                    }

                    String msg = "file format error: " + picName;
                    logger.Error( msg );
                    result.Add( msg );
                    return result;
                }

            }
            catch (Exception exception) {
                logger.Error( lang.get( "exPhotoUploadError" ) + ":" + exception.Message );
                result.Add( lang.get( "exPhotoUploadErrorTip" ) );
                return result;
            }
            result.Info = Path.GetFileName( Img.GetThumbPath( filename ) );
            return result;
        }
Exemplo n.º 8
0
        /// <summary>
        /// 保存缩略图到磁盘(可指定宽高,可指定缩放模式)
        /// </summary>
        /// <param name="srcPath"></param>
        /// <param name="destPath"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="mode"></param>
        public static void SaveThumbnail(String srcPath, String destPath, int width, int height, SaveThumbnailMode mode)
        {
            try {
                using (Image srcImg = Image.FromFile(srcPath)) {
                    ThumbSize t = getTargetSize(width, height, mode, srcImg);
                    using (Bitmap newImg = new Bitmap(t.New.Width, t.New.Height)) {
                        using (Graphics g = Graphics.FromImage(newImg)) {
                            g.InterpolationMode = InterpolationMode.High;
                            g.SmoothingMode     = SmoothingMode.HighQuality;
                            g.Clear(Color.White);
                            g.DrawImage(srcImg, t.getNewRect(), t.getRect(), GraphicsUnit.Pixel);

                            try {
                                newImg.Save(destPath, ImageFormat.Jpeg);
                            }
                            catch (Exception e) {
                                throw e;
                            }
                        }
                    }
                }
            }
            catch (OutOfMemoryException ex) {
                logger.Error("file format error: " + srcPath);
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// 上传图片(自定义保存路径),同时生成最小的缩略图
        /// </summary>
        /// <param name="uploadPath">保存路径(相对路径)</param>
        /// <param name="postedFile">HttpFile</param>
        /// <param name="picName">图片名称</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <returns></returns>
        public static Result SaveImg(String uploadPath, HttpFile postedFile, String picName, int width, int height, SaveThumbnailMode mode)
        {
            logger.Info("uploadPath : " + uploadPath);
            logger.Info("picName : " + picName);
            Result result = new Result();

            CheckUploadPic(postedFile, result);
            if (result.HasErrors)
            {
                return(result);
            }

            String str      = PathHelper.Map(uploadPath);
            String str2     = picName + "." + Img.GetImageExt(postedFile.ContentType);
            String filename = Path.Combine(str, str2);

            try {
                String oldFile = null;
                if (file.Exists(filename))
                {
                    oldFile = filename + "." + Guid.NewGuid() + Path.GetExtension(filename);
                    file.Move(filename, oldFile);
                }

                postedFile.SaveAs(filename);

                try {
                    saveThumbImagePrivate(filename, ThumbnailType.Small, width, height, mode);

                    if (strUtil.HasText(oldFile))
                    {
                        file.Delete(oldFile);
                    }
                }
                catch (OutOfMemoryException ex) {
                    file.Delete(filename);
                    if (strUtil.HasText(oldFile))
                    {
                        file.Move(oldFile, filename);
                    }

                    String msg = "file format error: " + picName;
                    logger.Error(msg);
                    result.Add(msg);
                    return(result);
                }
            }
            catch (Exception exception) {
                logger.Error(lang.get("exPhotoUploadError") + ":" + exception.Message);
                result.Add(lang.get("exPhotoUploadErrorTip"));
                return(result);
            }
            result.Info = Path.GetFileName(Img.GetThumbPath(filename));
            return(result);
        }
Exemplo n.º 10
0
 private static void saveThumbImagePrivate(String filename, ThumbnailType ttype, int x, int y, SaveThumbnailMode sm)
 {
     using (Image img = Image.FromFile(filename)) {
         if (img.Size.Width <= x && img.Size.Height <= y)
         {
             File.Copy(filename, Img.GetThumbPath(filename, ttype));
         }
         else
         {
             Img.SaveThumbnail(filename, Img.GetThumbPath(filename, ttype), x, y, sm);
         }
     }
 }
Exemplo n.º 11
0
Arquivo: Img.cs Projeto: jilumvc/Sajoo
        private static ThumbSize getCutSize( int width, int height, SaveThumbnailMode mode, Image src )
        {
            ThumbSize t = new ThumbSize();

            if ((double)src.Width / (double)src.Height > (double)width / (double)height) {

                int newWidth = src.Height * width / height;
                t.Src = new Size( newWidth, src.Height );
                t.New = new Size( width, height );
                t.Point = new Point( (src.Width - newWidth) / 2, 0 );
            }
            else {

                int newHeight = src.Width * height / width;
                t.Src = new Size( src.Width, newHeight );
                t.New = new Size( width, height );
                t.Point = new Point( 0, (src.Height - newHeight) / 2 );

            }

            return t;
        }
Exemplo n.º 12
0
Arquivo: Img.cs Projeto: jilumvc/Sajoo
        private static ThumbSize getAutoSize( int width, int height, SaveThumbnailMode mode, Image src )
        {
            ThumbSize t = new ThumbSize();
            t.Src = new Size( src.Width, src.Height );
            t.Point = new Point( 0, 0 );

            int newWidth = width;
            int newHeight = height;
            if ((double)src.Width / (double)src.Height > (double)width / (double)height)
                newHeight = src.Height * width / src.Width;
            else
                newWidth = src.Width * height / src.Height;
            t.New = new Size( newWidth, newHeight );

            return t;
        }
Exemplo n.º 13
0
Arquivo: Img.cs Projeto: jilumvc/Sajoo
        /// <summary>
        /// 保存缩略图到磁盘(可指定宽高,可指定缩放模式)
        /// </summary>
        /// <param name="srcPath"></param>
        /// <param name="destPath"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="mode"></param>
        public static void SaveThumbnail( String srcPath, String destPath, int width, int height, SaveThumbnailMode mode )
        {
            using (Image srcImg = Image.FromFile( srcPath )) {

                ThumbSize t = getTargetSize( width, height, mode, srcImg );
                using (Bitmap newImg = new Bitmap( t.New.Width, t.New.Height )) {

                    using (Graphics g = Graphics.FromImage( newImg )) {
                        g.InterpolationMode = InterpolationMode.High;
                        g.SmoothingMode = SmoothingMode.HighQuality;
                        g.Clear( Color.Transparent );
                        g.DrawImage( srcImg, t.getNewRect(), t.getRect(), GraphicsUnit.Pixel );

                        try {
                            newImg.Save( destPath, ImageFormat.Jpeg );
                        }
                        catch (Exception e) {
                            throw e;
                        }

                    }
                }
            }
        }
Exemplo n.º 14
0
Arquivo: Img.cs Projeto: jilumvc/Sajoo
        /// <summary>
        /// 获取缩略图尺寸
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="mode"></param>
        /// <param name="src"></param>
        /// <returns></returns>
        public static ThumbSize getTargetSize( int width, int height, SaveThumbnailMode mode, Image src )
        {
            ThumbSize t = new ThumbSize();
            t.Src = new Size( src.Width, src.Height );
            t.Point = new Point( 0, 0 );

            if (mode == SaveThumbnailMode.ByWidth) {
                int newHeight = src.Height * width / src.Width;
                t.New = new Size( width, newHeight );
                return t;
            }
            else if (mode == SaveThumbnailMode.ByHeight) {
                int newWidth = src.Width * height / src.Height;
                t.New = new Size( newWidth, height );
                return t;
            }
            else if (mode == SaveThumbnailMode.ByWidthHeight) {
                t.New = new Size( width, height );
                return t;
            }
            else if (mode == SaveThumbnailMode.Cut) {
                return getCutSize( width, height, mode, src );
            }

            return getAutoSize( width, height, mode, src );
        }