照片上传(缩略图实现)

1.获取所有的提交到服务器的文件集合

HttpFileCollection fileColl= Request.Files;

2.取得一个文件(这里是一张照片)

    HttpPostedFile pic = fileColl[0];

3.判断文件是否为空

  •     1.获取服务器存放图片的物理路径(Server.MapPath)

       string imgPhyPath = Server.MapPath("/upload/img/");  

  •     2.将图片保存到物理路径 (pic.fileName)   

    取出图片名称:  

      string filename = pic.FileName;  

      1.获取文件的扩展名 (path.GetExtension)

           string extName = System.IO.Path.GetExtension(fileName);

        2.随机生成1个唯一的文件名Guid.NewGuid())

          string newFile = Guid.NewGuid() + extName;   

      3.将文件保存到目标路径下:(upload\img\)

           pic.SaveAs(imgPhyPath + newfile);

  •     3.产生缩略图

      方法一(微软提供的,有bug)

          1.将浏览器上传的图片加载到图片对象中

             Image oldImg=Image.FromStream(pic.InputStream)

          2.获取原始图片的100*100的缩略图,GetThumbnailImage()方法不能产生所有图片的缩略图(bug)

             Image thumImg = oldImg.GetThumbnailImage(100, 100, null, IntPtr.Zero)

          3.将缩略图保存到(upload\thum\)

             string thumphyPath = Server.MapPath("/upload/thum/");

         4.获取缩略图的完整路径

                    string thumFullPath = thumphyPath + newfile;

                    thumImg.Save(thumFullPath);

    方法二 (自己动手,丰衣足食)

         1.将浏览器上传的图片加载到图片对象中

            Image oldImg=Image.FromStream(pic.InputStream)

         2.创建位图用于缩略图    

          Image thumImg = new Bitmap(100, 100)

         3.利用画家对象绘制缩略图

            Graphics g = Graphics.FromImage(thumImg)

         4.绘制缩略图     

         g.DrawImage(oldImg //要画的图片

                   , new Rectangle(0, 0, 100, 100)  //表示要画到缩略图的哪个位置(画满整个缩略图)

                  , new Rectangle(0, 0, oldImg.Width, oldImg.Height) //将原图整个画到缩略图

                  , GraphicsUnit.Pixel); //指定单位是像素   

       5.将缩略图保存到服务器的磁盘(upload\thum\)

           string thumphyPath = Server.MapPath("/upload/thum/");    

      6.获取缩略图的完整路径     string thumFullPath = thumphyPath + newfile;   

        7.保存图片     thumImg.Save(thumFullPath);

完整代码:

方法一:利用微软提供的方法GetThumbnailImage(100, 100, null, IntPtr.Zero)(不推荐使用

using (Image oldImg = Image.FromStream(file.InputStream))

{

    //获取原始图片的100 * 100的缩略图,GetThumbnailImage()方法不能够产生所有图片的缩略图

    using (Image thumImg = oldImg.GetThumbnailImage(100, 100, null, IntPtr.Zero))

    {

        //将缩略图保存到 upload\thum\

        string thumphyPath = Server.MapPath("/upload/thum/");

        string thumFullPath = thumphyPath + newfile; //获取缩略图的完整路径

        thumImg.Save(thumFullPath);

    }

} 


方法二:自己动手实现

if (Request.HttpMethod.ToLower() == "post")

{

    HttpFileCollection fileColl= Request.Files;

    HttpPostedFile pic = fileColl[0];

    if (pic != null)

    {

        #region 1.0 修改图片名称

        string imgPhyPath = Server.MapPath("/upload/img/");

        string fileName = pic.FileName;

        //获取文件的扩展名

        string extName = System.IO.Path.GetExtension(fileName);

        //生成新的文件名

        string newFile = Guid.NewGuid() + extName;

        //将文件保存到目标路径下

        pic.SaveAs(imgPhyPath+newFile); 

        #endregion



        #region 2.0 生成缩略图

        //创建image对象保存原图

        using (Image oldImg = Image.FromStream(pic.InputStream))

        {

            using (Image thumImg = new Bitmap(100, 100))

            {

                using (Graphics g = Graphics.FromImage(thumImg))

                {

                    g.DrawImage(

                        oldImg

                        ,

                        new Rectangle(0,0,100,100)

                        ,

                        new Rectangle(0,0,oldImg.Width,oldImg.Height)

                        ,

                        GraphicsUnit.Pixel

                        );

                    string thumPhyPath = Server.MapPath("/upload/thum/");

                    string thumFullPath = thumPhyPath + newFile;

                    thumImg.Save(thumFullPath);

                }

            }

        }

    }

}

你可能感兴趣的:(缩略图)