让CS的头像支持GIF动画

博客介绍了CS中用户头像的处理方式,程序先将上传图片转换成Bitmap再缩放,导致GIF动画变静态图。针对用户反馈,改进处理方式,图片小于设定大小不处理,大于则缩放,同样会使图片变静态图。

在CS中用户上传的头像是要经过处理的,具体的参见CS代码Components\Resources.cs,程序将上传的图片先转换成Bitmap再进行缩放处理,这样,GIF动画便成了静态图了。

因为有几个用户提到了这个,所以,把头像处理稍微改了一下,如果用户上传的图片大小小于设定的大小的话,就不进行处理,否则进行缩放,所以,大于设定大小的图片也会变成静态图的。代码如下:

None.gifpublic static void UpdateAvatar (int userID, Stream postedFile)  
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    
// TODO : allow default image format to be controlled through admin configuration 
InBlock.gif    
// 
InBlock.gif
    ImageFormat format = ImageFormat.Jpeg; 
InBlock.gif
InBlock.gif    
// 上传图像的大小 
InBlock.gif    
// 
InBlock.gif
    int contentLength = (int)postedFile.Length; 
InBlock.gif
InBlock.gif    
// validate stream 
InBlock.gif
    if (contentLength <= 0
InBlock.gif        
return
InBlock.gif
InBlock.gif    
// resize image 
InBlock.gif
    MemoryStream image = ResizeImage (postedFile, SiteSettingsManager.GetSiteSettings().AvatarHeight, SiteSettingsManager.GetSiteSettings().AvatarWidth, format); 
InBlock.gif     
InBlock.gif    
// write the avatar to storage 
InBlock.gif
    CommonDataProvider dp = CommonDataProvider.Instance(); 
InBlock.gif     
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//* **************************************************************************** 
InBlock.gif        * 标题: 修改上传头像保存位置 
InBlock.gif        * 内容: 如果设置上传头像的保存位置为硬盘的话,则将头像保存在指定的位置。 
InBlock.gif        *       这里没有使用ResizeImage来把图像进行缩放,因为一旦缩放,GIF动画会变成 
InBlock.gif        *       静态图,这里结合JS来实现,具体参见UserAvatar代码。 
InBlock.gif        *       本来是参考宝玉的上传方式来做的,就是上传了图片之后,将cs_Images中的对 
InBlock.gif        *       应的行删掉,再把cs_UserAvatar表中的更新,我在实现的时候出了点问题,忘 
InBlock.gif        *       了它们之间的约束关系,后来想到了,但也没有做什么更改,只是把FileName字 
InBlock.gif        *       段移到了cs_Images表中。 
InBlock.gif        * 修改: 水村 
InBlock.gif        * 时间: 2005-04-11  
ExpandedSubBlockEnd.gif        * ***************************************************************************
*/
 
InBlock.gif    
if( SiteSettingsManager.GetSiteSettings().EnableSaveInDisk ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        Avatar avatar 
= new Avatar();                 
InBlock.gif
InBlock.gif        System.Drawing.Image avatarImage 
= Image.FromStream( postedFile ); 
InBlock.gif
InBlock.gif        
if( avatarImage.Width > SiteSettingsManager.GetSiteSettings().AvatarWidth || avatarImage.Height > SiteSettingsManager.GetSiteSettings().AvatarHeight ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            avatarImage 
= Image.FromStream( image ); 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
InBlock.gif        
string avatarPath = HttpContext.Current.Server.MapPath( SiteSettingsManager.GetSiteSettings().AvatarSavePath ); 
InBlock.gif        
string fileName = userID.ToString() + FileExt( avatarImage.RawFormat ); 
InBlock.gif
InBlock.gif        avatar.FileName 
= fileName; 
InBlock.gif        avatar.ContentType 
= "image/" + FileExt( avatarImage.RawFormat ).Substring(1); 
InBlock.gif        avatar.Length 
= contentLength; 
InBlock.gif         
InBlock.gif        
if!System.IO.Directory.Exists( avatarPath ) ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                System.IO.Directory.CreateDirectory( avatarPath ); 
ExpandedSubBlockEnd.gif            }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
catch dot.gif{ } 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif         
InBlock.gif        avatarImage.Save( avatarPath 
+ fileName ); 
InBlock.gif
InBlock.gif        dp.CreateUpdateDeleteImage(userID, avatar, DataProviderAction.Update); 
ExpandedSubBlockEnd.gif    }
 
InBlock.gif    
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        Avatar avatar 
= new Avatar(image); 
InBlock.gif        avatar.ContentType 
= "image/" + format.ToString(); 
InBlock.gif        dp.CreateUpdateDeleteImage(userID, avatar, DataProviderAction.Update); 
ExpandedSubBlockEnd.gif    }
 
ExpandedBlockEnd.gif}
 
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**////  
InBlock.gif
/// 返回图片的格式的后缀 
InBlock.gif
///  
InBlock.gif
/// 图片格式 
ExpandedBlockEnd.gif
/// 后缀字附串 

None.gifpublic static string FileExt( System.Drawing.Imaging.ImageFormat format ) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    
if( format == ImageFormat.Bmp ) return ".bmp"
InBlock.gif    
else if( format == ImageFormat.Gif ) return ".gif"
InBlock.gif    
else if( format == ImageFormat.Png ) return ".png"
InBlock.gif    
else if( format == ImageFormat.Jpeg ) return ".jpg"
InBlock.gif    
else return ".jpg"
ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/Jerryes/archive/2005/08/22/220209.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值