现在的位置: 首页 > 综合 > 正文

图片上传

2013年09月10日 ⁄ 综合 ⁄ 共 6542字 ⁄ 字号 评论关闭

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

代码如下: 

Java代码  收藏代码
  1. /// <summary>   
  2.    /// asp.net上传图片并生成缩略图   
  3.    /// </summary>   
  4.    /// <param name="upImage">HtmlInputFile控件</param>   
  5.    /// <param name="sSavePath">保存的路径,些为相对服务器路径的下的文件夹</param>   
  6.    /// <param name="sThumbExtension">缩略图的thumb</param>   
  7.    /// <param name="intThumbWidth">生成缩略图的宽度</param>   
  8.    /// <param name="intThumbHeight">生成缩略图的高度</param>   
  9.    /// <returns>缩略图名称</returns>   
  10.    public string UpLoadImage(HtmlInputFile upImage, string sSavePath, string sThumbExtension, int intThumbWidth, int intThumbHeight)   
  11.    {   
  12.        string sThumbFile = "";   
  13.        string sFilename = "";           
  14.   
  15.        if (upImage.PostedFile != null)   
  16.        {   
  17.            HttpPostedFile myFile = upImage.PostedFile;   
  18.            int nFileLen = myFile.ContentLength;   
  19.            if (nFileLen == 0)   
  20.                return "没有选择上传图片";               
  21.   
  22.            //获取upImage选择文件的扩展名   
  23.            string extendName = System.IO.Path.GetExtension(myFile.FileName).ToLower();   
  24.            //判断是否为图片格式   
  25.            if (extendName != ".jpg" && extendName != ".jpge" && extendName != ".gif" && extendName != ".bmp" && extendName != ".png")   
  26.                return "图片格式不正确";   
  27.               
  28.   
  29.            byte[] myData = new Byte[nFileLen];   
  30.            myFile.InputStream.Read(myData, 0, nFileLen);   
  31.   
  32.            sFilename = System.IO.Path.GetFileName(myFile.FileName);   
  33.            int file_append = 0;   
  34.            //检查当前文件夹下是否有同名图片,有则在文件名+1   
  35.            while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename)))   
  36.            {   
  37.                file_append++;   
  38.                sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)   
  39.                    + file_append.ToString() + extendName;   
  40.            }   
  41.            System.IO.FileStream newFile   
  42.                = new System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename),   
  43.                System.IO.FileMode.Create, System.IO.FileAccess.Write);   
  44.            newFile.Write(myData, 0, myData.Length);   
  45.            newFile.Close();   
  46.   
  47.            //以上为上传原图   
  48.   
  49.            try   
  50.            {   
  51.                //原图加载   
  52.                using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename)))   
  53.                {   
  54.                    //原图宽度和高度   
  55.                    int width = sourceImage.Width;   
  56.                    int height = sourceImage.Height;   
  57.                    int smallWidth;   
  58.                    int smallHeight;   
  59.   
  60.                    //获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽  和 原图的高/缩略图的高)   
  61.                    if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight)   
  62.                    {   
  63.                        smallWidth = intThumbWidth;   
  64.                        smallHeight = intThumbWidth * height / width;   
  65.                    }   
  66.                    else   
  67.                    {   
  68.                        smallWidth = intThumbHeight * width / height;   
  69.                        smallHeight = intThumbHeight;   
  70.                    }   
  71.   
  72.                    //判断缩略图在当前文件夹下是否同名称文件存在   
  73.                    file_append = 0;   
  74.                    sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + extendName;   
  75.   
  76.                    while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sThumbFile)))   
  77.                    {   
  78.                        file_append++;   
  79.                        sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) +   
  80.                            file_append.ToString() + extendName;   
  81.                    }   
  82.                    //缩略图保存的绝对路径   
  83.                    string smallImagePath = System.Web.HttpContext.Current.Server.MapPath(sSavePath) + sThumbFile;   
  84.   
  85.                    //新建一个图板,以最小等比例压缩大小绘制原图   
  86.                    using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight))   
  87.                    {   
  88.                        //绘制中间图   
  89.                        using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))   
  90.                        {   
  91.                            //高清,平滑   
  92.                            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;   
  93.                            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;   
  94.                            g.Clear(Color.Black);   
  95.                            g.DrawImage(   
  96.                            sourceImage,   
  97.                            new System.Drawing.Rectangle(00, smallWidth, smallHeight),   
  98.                            new System.Drawing.Rectangle(00, width, height),   
  99.                            System.Drawing.GraphicsUnit.Pixel   
  100.                            );   
  101.                        }   
  102.                        //新建一个图板,以缩略图大小绘制中间图   
  103.                        using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight))   
  104.                        {   
  105.                            //绘制缩略图   
  106.                            using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1))   
  107.                            {   
  108.                                //高清,平滑   
  109.                                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;   
  110.                                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;   
  111.                                g.Clear(Color.Black);   
  112.                                int lwidth = (smallWidth - intThumbWidth) / 2;   
  113.                                int bheight = (smallHeight - intThumbHeight) / 2;   
  114.                                g.DrawImage(bitmap, new Rectangle(00, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel);   
  115.                                g.Dispose();   
  116.                                bitmap1.Save(smallImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);   
  117.                            }   
  118.                        }   
  119.                    }   
  120.                }   
  121.            }   
  122.            catch   
  123.            {   
  124.                //出错则删除   
  125.                System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename));   
  126.                return "图片格式不正确";   
  127.            }   
  128.            //返回缩略图名称   
  129.            return sThumbFile;   
  130.        }   
  131.        

抱歉!评论已关闭.