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

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
L
LangChain Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recent Announcements
Recent Announcements
大猫的无限游戏
大猫的无限游戏
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
博客园 - 【当耐特】
博客园 - 叶小钗
I
InfoQ
MyScale Blog
MyScale Blog
H
Help Net Security
月光博客
月光博客
Vercel News
Vercel News
B
Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
D
DataBreaches.Net
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
Netflix TechBlog - Medium
宝玉的分享
宝玉的分享
WordPress大学
WordPress大学
GbyAI
GbyAI
Blog — PlanetScale
Blog — PlanetScale
博客园 - Franky

博客园 - cowboy

图片上传到图片服务器 程序员有多少喜欢李嘉欣的,今天在程序朋友的一篇博客看到这样的文章,使我大跌眼镜....... 很长时间没有来博客园了,今天来博客园记录几件大事 获取MSN在线好友-Santorini项目插件 - cowboy - 博客园 搜索引擎指南robots.txt文件 Google SiteMap的作用及协议格式详解 发现了.net 2.0框架的BUG了,page_load执行了2次 正则表达式转义 - cowboy - 博客园 由于自己开发的项目需要正则表达式,这里记录一下,以便查阅 Windows Server 2003 安全配置 获取高精度的时间差,可以用来分析页面运行时间的长短 韩正市长来参观 和韩正市长握手 盖茨6-16宣布2008年7月开始不再负责公司的日常管理. 大众分类法,物以类聚,人以群分。 css+div布局总结--新手入门 采用DIV+CSS布局的好处 让输入框关闭自动完成(AutoComplete)功能 今天要为自己的网站添加一个动态检索信息的下拉提示框,这个是在网上找的例子。
asp.net生成缩略图通用函数(支持多种生成方式) 支持图片裁减
cowboy · 2006-07-04 · via 博客园 - cowboy

Posted on 2006-07-04 20:14  cowboy  阅读(635)  评论()    收藏  举报

/**//// <summary>
        
/// 生成缩略图
        
/// </summary>
        
/// <param name="originalImagePath">源图路径(物理路径)</param>
        
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
        
/// <param name="width">缩略图宽度</param>
        
/// <param name="height">缩略图高度</param>
        
/// <param name="mode">生成缩略图的方式</param>    

        public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
        
{
            Image originalImage 
= Image.FromFile(originalImagePath);
            
            
int towidth = width;
            
int toheight = height;
        
            
int x = 0;
            
int y = 0;
            
int ow = originalImage.Width;
            
int oh = originalImage.Height;        

            
switch (mode)
            
{        
                
case "HW"://指定高宽缩放(可能变形)                
                    break;
                
case "W"://指定宽,高按比例                    
                    toheight = originalImage.Height * width/originalImage.Width;
                    
break;
                
case "H"://指定高,宽按比例
                    towidth = originalImage.Width * height/originalImage.Height;                    
                    
break;        
                
case "Cut"://指定高宽裁减(不变形)                
                    if((double)originalImage.Width/(double)originalImage.Height > (double)towidth/(double)toheight)
                    
{
                        oh 
= originalImage.Height;
                        ow 
= originalImage.Height*towidth/toheight;
                        y 
= 0;
                        x 
= (originalImage.Width - ow)/2;
                    }

                    
else
                    
{
                        ow 
= originalImage.Width;
                        oh 
= originalImage.Width*height/towidth;
                        x 
= 0;
                        y 
= (originalImage.Height - oh)/2;
                    }

                    
break;                    
                
default :
                    
break;
            }
    
            
            
//新建一个bmp图片
            Image bitmap = new System.Drawing.Bitmap(towidth,toheight);

            
//新建一个画板
            Graphics g = System.Drawing.Graphics.FromImage(bitmap);

            
//设置高质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

            
//设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            
//清空画布并以透明背景色填充
            g.Clear(Color.Transparent);        

            
//在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(originalImage, new Rectangle(00, towidth, toheight), 
                
new Rectangle(x, y, ow,oh),
                GraphicsUnit.Pixel);

            
try
            
{            
                
//以jpg格式保存缩略图
                bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            }

            
catch(System.Exception e)
            
{
                
throw e;
            }

            
finally
            
{
                originalImage.Dispose();
                bitmap.Dispose();                        
                g.Dispose();
            }

        }