网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

关于图片缩放的c#方法-创新互联

  在网站建设中,关于图片是必不可少的,后台管理中往往需要上传图片,大的图片在网络中传输速率很慢,很不理想,因此解决办法是,用户上传图片时候,保存一个图片的缩略图,在网页显示用缩略图,用户下载,使用原图,下面是通过搜索资料,整理的c#关于保存图片缩略图的方法,用户只需要传入适当参数,调用此方法就可以生成缩略图。

创新互联公司是一家集网站建设,湘乡企业网站建设,湘乡品牌网站建设,网站定制,湘乡网站建设报价,网络营销,网络优化,湘乡网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.IO;   
    public partial class upload : System.Web.UI.Page 
    {     
        protected void Page_Load(object sender, EventArgs e)     
        {         
                 
        }     
    }  
    ///      
    /// 图片等比缩放     
    ///      
    /// 原图地址加名称     
    /// 缩略图存放地址     
    /// 缩略图名称     
    /// 指定的大宽度     
    /// 指定的大高度     
    public static void zoomauto(string initpath, string savepath,string smallname, double targetwidth, double targetheight)     
    {         
        //虚拟路径转绝对路径         
        initpath = System.Web.HttpContext.Current.Server.MapPath(initpath);         
        savepath = System.Web.HttpContext.Current.Server.MapPath(savepath);         
        //创建目录         
        string dir = Path.GetDirectoryName(savepath);         
        if (!Directory.Exists(dir))             
            Directory.CreateDirectory(dir);  
        
        //原始图片(获取原始图片创建对象,并使用流中嵌入的颜色管理信息)         
        System.Drawing.Image initp_w_picpath = System.Drawing.Image.FromFile(initpath);  
        //原图宽高均小于模版,不作处理,直接保存         
        if (initp_w_picpath.Width <= targetwidth && initp_w_picpath.Height <= targetheight)         
        {             
            //保存             
            initp_w_picpath.Save(savepath + smallname, System.Drawing.Imaging.ImageFormat.Jpeg);         
        }         
        else         
        {             
            //缩略图宽、高计算             
            double newwidth = initp_w_picpath.Width;             
            double newheight = initp_w_picpath.Height;  
            //宽大于高或宽等于高(横图或正方)             
            if (initp_w_picpath.Width > initp_w_picpath.Height || initp_w_picpath.Width == initp_w_picpath.Height)             
            {                 
                //如果宽大于模版                 
                if (initp_w_picpath.Width > targetwidth) 
                {                     
                    //宽按模版,高按比例缩放                     
                    newwidth = targetwidth;                     
                    newheight = initp_w_picpath.Height * (targetwidth / initp_w_picpath.Width);                 
                }             
            }             
            //高大于宽(竖图)             
            else             
            {                 
                //如果高大于模版                 
                if (initp_w_picpath.Height > targetheight)                 
                {                     
                    //高按模版,宽按比例缩放                     
                    newheight = targetheight;                     
                    newwidth = initp_w_picpath.Width * (targetheight / initp_w_picpath.Height);                 
                }             
            }  
            
            //生成新图             
            //新建一个bmp图片             
            System.Drawing.Image newp_w_picpath = new System.Drawing.Bitmap((int)newwidth, (int)newheight);             
            //新建一个画板             
            System.Drawing.Graphics newg = System.Drawing.Graphics.FromImage(newp_w_picpath);  
            //设置质量             
            newg.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;             
            newg.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;  
            //置背景色             
            newg.Clear(System.Drawing.Color.White);             
            //画图             
            newg.DrawImage(initp_w_picpath, new System.Drawing.Rectangle(0, 0, newp_w_picpath.Width, newp_w_picpath.Height), new System.Drawing.Rectangle(0, 0, initp_w_picpath.Width, initp_w_picpath.Height), System.Drawing.GraphicsUnit.Pixel);  
            //保存缩略图             
            newp_w_picpath.Save(savepath + smallname, System.Drawing.Imaging.ImageFormat.Jpeg);  
            //释放资源             
            newg.Dispose();             
            newp_w_picpath.Dispose();             
            initp_w_picpath.Dispose(); 
        
        }     
    }  
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章题目:关于图片缩放的c#方法-创新互联
地址分享:http://bjjierui.cn/article/cejcei.html

其他资讯