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

推荐订阅源

N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
F
Fortinet All Blogs
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Stack Overflow Blog
Stack Overflow Blog
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
LangChain Blog
Microsoft Security Blog
Microsoft Security Blog
Apple Machine Learning Research
Apple Machine Learning Research
Y
Y Combinator Blog
阮一峰的网络日志
阮一峰的网络日志
博客园_首页
IT之家
IT之家
V
V2EX
C
Check Point Blog
MongoDB | Blog
MongoDB | Blog
Last Week in AI
Last Week in AI
B
Blog
J
Java Code Geeks
大猫的无限游戏
大猫的无限游戏
雷峰网
雷峰网

博客园 - 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();
            }
        }
    }