惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
Y
Y Combinator Blog
Recorded Future
Recorded Future
博客园 - Franky
I
InfoQ
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
P
Palo Alto Networks Blog
G
GRAHAM CLULEY
Cloudbric
Cloudbric
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
F
Full Disclosure
Google DeepMind News
Google DeepMind News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Check Point Blog
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
T
Threat Research - Cisco Blogs
U
Unit 42
N
Netflix TechBlog - Medium
The Cloudflare Blog
Spread Privacy
Spread Privacy
Microsoft Azure Blog
Microsoft Azure Blog
美团技术团队
T
Troy Hunt's Blog
Engineering at Meta
Engineering at Meta
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tenable Blog
B
Blog
S
Securelist
H
Hacker News: Front Page
Google Online Security Blog
Google Online Security Blog
G
Google Developers Blog

博客园 - 老枪

sql 修改时间 接近完美的CSS标题、列表溢出隐藏 - 老枪 - 博客园 通过js读取或设置FCKeditor中的值 - 老枪 - 博客园 access查找表是否存在 css 如何设置 某div 里面的所有图片的最大宽度。当加入文章里图片时,图片自动按比例减小。 Asp.net把UTF-8编码转换为GB2312编码 检索COM类工厂中CLSID...”的两个错误的解决方法 检索COM类工厂中CLSID...”的两个错误的解决方法 111111 Lucene基于Java的全文检索引擎简介 sql2005 中文分词 - 老枪 - 博客园 gridview使用大全 ASP.NET程序中常用的三十三种代码 - 老枪 - 博客园 使用System.Web.Mail通过需验证的邮件服务器发送邮件 - 老枪 - 博客园 40种网页常用小技巧(javascript) - 老枪 - 博客园 ASP.NET替换HTML代码<>中的任意代码 - 老枪 - 博客园 SQL SERVER 2005 同步复制技术 网页制作之在线视频播放代码 - 老枪 - 博客园 常用javascript数据验 - 老枪 - 博客园 asp.net2.0中,使用gridview显示新闻标题
高素质图片压缩
老枪 · 2009-12-24 · via 博客园 - 老枪

高素质图片压缩

  • #region Using Directives   
  •   
  • using System;   
  • using System.Web;   
  • using System.Drawing;   
  • using System.Drawing.Imaging;   
  • using System.Drawing.Drawing2D;  
  •  
  • #endregion Using Directives   
  •   
  • public class ImageResizer   
  • {  
  •     #region Fields   
  •   
  •     static HttpServerUtility Server = HttpContext.Current.Server;  
  •  
  •     #endregion Fields  
  •  
  •     #region Get Encoder   
  •   
  •     public static ImageCodecInfo GetEncoder(string mimeType)   
  •     {   
  •         foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageEncoders())   
  •         {   
  •             if (codec.MimeType == mimeType)   
  •             {   
  •                 return codec;   
  •             }   
  •         }   
  •   
  •         return null;   
  •     }  
  •  
  •     #endregion Get Encoder  
  •  
  •     #region Resize Image   
  •   
  •     public static void ResizeImage(string originalImageLocation,    
  •                   int width, int height, string newImageLocation)   
  •     {   
  •         Image originalImage = Image.FromFile(   
  •           Server.MapPath(originalImageLocation.Replace("//""/")));   
  •         ResizeImage(originalImage, width, height, newImageLocation);   
  •     }   
  •   
  •     public static void ResizeImage(Image originalImage,    
  •                   int width, int height, string newImageLocation)   
  •     {   
  •           
  •           
  •   
  •         decimal newWidth;   
  •         decimal newHeight;   
  •   
  •         if ((originalImage.Width / width) > (originalImage.Width / height))   
  •         {   
  •             newWidth = width;   
  •             newHeight = (Int32)Math.Ceiling(Convert.ToDecimal((   
  •                            originalImage.Height * newWidth) / originalImage.Width));   
  •             if (newHeight > height)   
  •             {   
  •                 newWidth = newWidth * (height / newHeight);   
  •                 newHeight = height;   
  •             }   
  •         }   
  •         else  
  •         {   
  •             newHeight = height;   
  •             newWidth = Math.Ceiling(Convert.ToDecimal((   
  •                           originalImage.Width * newHeight) / originalImage.Height));   
  •   
  •             if (newWidth > width)   
  •             {   
  •                 newHeight = newHeight * (width / newWidth);   
  •                 newWidth = width;   
  •             }   
  •         }   
  •   
  •           
  •           
  •   
  •         Bitmap newBitmap = new Bitmap(Convert.ToInt32(newWidth),    
  •                    Convert.ToInt32(newHeight), PixelFormat.Format16bppRgb565);   
  •         Graphics g = Graphics.FromImage(newBitmap);   
  •         g.InterpolationMode = InterpolationMode.HighQualityBicubic;   
  •         g.SmoothingMode = SmoothingMode.AntiAlias;   
  •         g.SmoothingMode = SmoothingMode.HighQuality;   
  •         g.DrawImage(originalImage, 0, 0, Convert.ToInt32(newWidth),    
  •                     Convert.ToInt32(newHeight));   
  •   
  •           
  •           
  •   
  •         ImageCodecInfo codecEncoder = ImageResizer.GetEncoder("image/jpeg");   
  •         int quality = 98;   
  •         EncoderParameters encodeParams = new EncoderParameters(1);   
  •         EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, quality);   
  •         encodeParams.Param[0] = qualityParam;   
  •         newBitmap.SetResolution(72, 72);   
  •         newBitmap.Save(Server.MapPath(newImageLocation), codecEncoder, encodeParams);   
  •   
  •           
  •           
  •   
  •         g.Dispose();   
  •         newBitmap.Dispose();   
  •         originalImage.Dispose();   
  •     }  
  •  
  •     #endregion Resize Image   
  • posted on 2009-12-24 14:09  老枪  阅读(295)  评论()    收藏  举报