我的网站 天气在线 最近随着寒潮的来临,生意越来越好了!但作为站长也要发愁啊!因为空间那点流量(每月80G)很快就不够用了!
除了升级空间,我又想了一个办法。就是减少其中的图片的尺寸。
其实,有很多原图是挺大的,比如有200KB,但展示在网页里是比较小的。那我何不将其缩小呢?
在网上找了不少的代码,都不能直接使用。综合了一下,终于调试成功!
参数说明:sName: 原文件名
sThumb 缩略文件名
nWidth 生成的图片的尺寸。如果原图更小,则保留原图不变。
在生成后,我是将原文件删除了的。
public static void CreateThumbImage( string sName, string sThumb,int nWidth){if (File.Exists(sThumb)){File.Delete(sThumb);}else{// 确保目录存在// CDirectory.CreateIfNotExist(Path.GetDirectoryName(sThumb));}//Image.GetThumbnailImageAbort myCallback = ThumbnailCallback;Bitmap bitmap = new Bitmap(sName);int width = bitmap.Width;int height = bitmap.Height;int w2=width;int h2=height;if( width>nWidth){w2 = nWidth;h2 = height*nWidth/width;var thumbnail = bitmap.GetThumbnailImage(w2, h2, null, IntPtr.Zero);bitmap.Dispose();thumbnail.Save(sThumb, System.Drawing.Imaging.ImageFormat.Jpeg);}else{bitmap.Dispose();File.Move(sName, sThumb);}try{File.Delete(sName);}catch (Exception){}}
本文发布于:2024-02-01 21:42:07,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170679492939594.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |