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

推荐订阅源

Google DeepMind News
Google DeepMind News
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
美团技术团队
The Cloudflare Blog
云风的 BLOG
云风的 BLOG
WordPress大学
WordPress大学
A
About on SuperTechFans
D
DataBreaches.Net
博客园_首页
月光博客
月光博客
MongoDB | Blog
MongoDB | Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Proofpoint News Feed
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
B
Blog
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
T
Tailwind CSS Blog
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
宝玉的分享
宝玉的分享

博客园 - Eric Yao

当前几个主要的Lucene中文分词器的比较 jquery radio,checkbox,select操作 - Eric Yao .NET开发十大必备工具 基于.net开发平台项目案例集锦 实现FCKeditor 多用户分文件夹上传图片等附件 网络蜘蛛C#开源示例 全球知名CMS系统清单 网站优化中的多域名问题 计算机端口大全 简化软件操作,提升用户体验 对于URL重写,支持无后缀url请求 切换CSS换皮肤 Asp.net关于Header/title/Meta tages/Style操作的小技巧 UML学习笔记(2) UML学习笔记(1) 对面向对象设计原则的总结 修改 3389 端口终端 十二款世界顶级杀毒软件下载---有序列号全可免费升级 收集得最全的sql 语句
GDI+中对图片的裁剪
Eric Yao · 2006-12-02 · via 博客园 - Eric Yao

void CutImage(HttpPostedFile post,string ppuid,out string imagename)
    {
        System.Drawing.Image SourceImg = System.Drawing.Image.FromStream(post.InputStream);
        if (SourceImg.Height > ConfigHelper.UserFaceMaxHeight)
        {
            this._lbl_upload_msg.Text = "最大高度不得大于 " + ConfigHelper.UserFaceMaxHeight;
            return;
        }
        if (SourceImg.Width > ConfigHelper.UserFaceMaxWidth)
        {
            this._lbl_upload_msg.Text = "最大宽度不得大于 " + ConfigHelper.UserFaceMaxWidth;
            return;
        }
        ImageFormat format = getImageformat(System.IO.Path.GetExtension(post.FileName));
        string filename =  ppuid+"."+format.ToString();
        imagename = filename;

        if (!UserFaceDir.EndsWith("\\")) 
            UserFaceDir = UserFaceDir+"\\";
        filename = UserFaceDir + filename;
        int SourceImgWidth = SourceImg.Width;
        int SourceImgHeight = SourceImg.Height;
        if ((SourceImgWidth != ConfigHelper.UserFaceWidth) && (SourceImgHeight != ConfigHelper.UserFaceHeight))
        {
            //如果宽高比例为1:1,则直接构成缩略图
            if (((Double)SourceImgWidth / SourceImgHeight) == 1)
            {
                System.Drawing.Image thumbimg = SourceImg.GetThumbnailImage(ConfigHelper.UserFaceWidth, ConfigHelper.UserFaceHeight, null, IntPtr.Zero);
                thumbimg.Save(filename, format);
                thumbimg.Dispose();
                SourceImg.Dispose();
                return;
            }
            Bitmap bit = new Bitmap(SourceImg);
            Rectangle rec = new Rectangle();   //构造一个Rectangle类,一个矩形
            rec.Width = ConfigHelper.UserFaceWidth;  
            rec.Height = ConfigHelper.UserFaceHeight;
            if (SourceImgWidth > rec.Width)
                rec.X = (SourceImgWidth - rec.Width) / 2;
            else
            {
                rec.X = 0;
                rec.Width = SourceImg.Width;
            }
            if (SourceImgHeight > rec.Height)
                rec.Y = (SourceImgHeight - rec.Height) / 2;
            else
            {
                rec.Y = 0;
                rec.Height = SourceImg.Height;
            }

            try
            {
                //这里就是把从上传过程中构造的bitmap克隆一份,并按定义好的矩形裁剪
                bit.Clone(rec, PixelFormat.DontCare).Save(filename, format);
            }
            catch (Exception ex)
            {
                this._lbl_upload_msg.Text = ex.Message;
                return;
            }
            finally
            {
                bit.Dispose();
                SourceImg.Dispose();
            }
        }
    }