asp.net上传图片生成缩略图

在asp.net中,上传图片功能或者是常用的,生成缩略图也是常用的。baidu或者google,c#的方法也是很多的,但是一用却发现缩略图不清晰啊,缩略图片太大之类的事情,下面是我在处理图片上的代码,效果不错,所以拿出来分享,(效果能达到一些绘图软件的效果)

代码如下:

001///  
002    /// asp.net上传图片并生成缩略图 
003    ///  
004    /// HtmlInputFile控件 
005    /// 保存的路径,些为相对服务器路径的下的文件夹 
006    /// 缩略图的thumb 
007    /// 生成缩略图的宽度 
008    /// 生成缩略图的高度 
009    /// 缩略图名称 
010    public string UpLoadImage(HtmlInputFile upImage, string sSavePath, string sThumbExtension, int intThumbWidth, int intThumbHeight) 
011    { 
012        string sThumbFile = ""
013        string sFilename = "";        
014        if (upImage.PostedFile != null
015        { 
016            HttpPostedFile myFile = upImage.PostedFile; 
017            int nFileLen = myFile.ContentLength; 
018            if (nFileLen == 0) 
019                return "没有选择上传图片";            
020            //获取upImage选择文件的扩展名 
021            string extendName = System.IO.Path.GetExtension(myFile.FileName).ToLower(); 
022            //判断是否为图片格式 
023            if (extendName != ".jpg" && extendName != ".jpge" && extendName != ".gif" && extendName != ".bmp" && extendName != ".png"
024                return "图片格式不正确"
025              
026            byte[] myData = new Byte[nFileLen]; 
027            myFile.InputStream.Read(myData, 0, nFileLen);
028            sFilename = System.IO.Path.GetFileName(myFile.FileName); 
029            int file_append = 0; 
030            //检查当前文件夹下是否有同名图片,有则在文件名+1 
031            while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename))) 
032            { 
033                file_append++; 
034                sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) 
035                    + file_append.ToString() + extendName; 
036            } 
037            System.IO.FileStream newFile 
038                = new System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename), 
039                System.IO.FileMode.Create, System.IO.FileAccess.Write); 
040            newFile.Write(myData, 0, myData.Length); 
041            newFile.Close();
042            //以上为上传原图
043            try 
044            { 
045                //原图加载 
046                using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename))) 
047                { 
048                    //原图宽度和高度 
049                    int width = sourceImage.Width; 
050                    int height = sourceImage.Height; 
051                    int smallWidth; 
052                    int smallHeight;
053                    //获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽 和 原图的高/缩略图的高) 
054                    if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight) 
055                    { 
056                        smallWidth = intThumbWidth; 
057                        smallHeight = intThumbWidth * height / width; 
058                    } 
059                    else 
060                    { 
061                        smallWidth = intThumbHeight * width / height; 
062                        smallHeight = intThumbHeight; 
063                    }
064                    //判断缩略图在当前文件夹下是否同名称文件存在 
065                    file_append = 0; 
066                    sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + extendName;
067                    while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sThumbFile))) 
068                    { 
069                        file_append++; 
070                        sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + 
071                            file_append.ToString() + extendName; 
072                    } 
073                    //缩略图保存的绝对路径 
074                    string smallImagePath = System.Web.HttpContext.Current.Server.MapPath(sSavePath) + sThumbFile;
075                    //新建一个图板,以最小等比例压缩大小绘制原图 
076                    using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight)) 
077                    { 
078                        //绘制中间图 
079                        using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap)) 
080                        { 
081                            //高清,平滑 
082                            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 
083                            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
084                            g.Clear(Color.Black); 
085                            g.DrawImage( 
086                            sourceImage, 
087                            new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight), 
088                            new System.Drawing.Rectangle(0, 0, width, height), 
089                            System.Drawing.GraphicsUnit.Pixel 
090                            ); 
091                        } 
092                        //新建一个图板,以缩略图大小绘制中间图 
093                        using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight)) 
094                        { 
095                            //绘制缩略图 
096                            using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1)) 
097                            { 
098                                //高清,平滑 
099                                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 
100                                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
101                                g.Clear(Color.Black); 
102                                int lwidth = (smallWidth - intThumbWidth) / 2; 
103                                int bheight = (smallHeight - intThumbHeight) / 2; 
104                                g.DrawImage(bitmap, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel); 
105                                g.Dispose(); 
106                                bitmap1.Save(smallImagePath, System.Drawing.Imaging.ImageFormat.Jpeg); 
107                            } 
108                        } 
109                    } 
110                } 
111            } 
112            catch 
113            { 
114                //出错则删除 
115                System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename)); 
116                return "图片格式不正确"
117            } 
118            //返回缩略图名称 
119            return sThumbFile; 
120        } 
121        return "没有选择图片"
122    }

 

HtmlInputFile控件我想大家都应该知道的,就是input type=file....这个会在后面的一起学asp.net章节中讲解

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-672870/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/12639172/viewspace-672870/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值