照片上传(缩略图实现)

摘要:
1.获取提交到服务器HttpFileCollectionfileCollection=Request的所有文件集合。文件夹;2.获取一个文件(这里是一张照片)HttpPostedFilepic=fileColl[0];3.判断文件是否为空。1.获取物理路径(Server.MapPath)stringmgPhyPath=Server。MapPath(“/upload/img

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.将文件保存到目标路径下:(uploadimg)

           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 hum)

             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 hum)

           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	hum
        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);
                }
            }
        }
    }
}

免责声明:文章转载自《照片上传(缩略图实现)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇《中小学生Python编程入门指南》前言360急速浏览器JS的调试下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

MySqlHelper、CacheHelper

MySqlHelper代码: using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using M...

Hive metastore源码阅读(一)

不要问我为什么,因为爱,哈哈哈哈。。。进入正题,最近做项目顺带学习了下hive metastore的源码,进行下知识总结。 hive metastore的整体架构如图: 一、组成结构: 如图我们可以看到,hive metastore的组成结构分为 客户端 服务端 ,那么下来我们逐一进行分析: 1、客户端 从代码的角度来看:尼玛太多了。。我们从入口HIV...

SpringBoot+ElementUI实现通用文件下载请求(全流程图文详细教程)

场景 在某些场景下需要前端浏览器从服务器端下载文件,比如需要下载导入Excel的模板。 注: 博客: https://blog.csdn.net/badao_liumang_qizhi关注公众号 霸道的程序猿 获取编程相关电子书、教程推送与免费下载。 实现 既然是实现通用下载接口,就要实现在后端配置一个下载文件的路径,在前端进行下载请求时传递要下载的文件...

java 实现基于opencv全景图合成

因项目需要,自己做了demo,从中学习很多,所以分享出来,希望有这方面需求的少走一些弯路,opencv怎么安装网上教程多多,这里不加详细说明,我安装的opencv-3.3.0  如上图所示,找到相应的jar包,这里讲一下如何这个jar如何导入Maven仓库 mvn install:install-file -Dfile=D:opencv-3.0.0ope...

PHP类和对象函数实例详解

1. interface_exists、class_exists、method_exists和property_exists:       顾名思义,从以上几个函数的命名便可以猜出几分他们的功能。我想这也是我随着对PHP的深入学习而越来越喜欢这门编程语言的原因了吧。下面先给出他们的原型声明和简短说明,更多的还是直接看例子代码吧。bool interface...

cJSON 使用详解

由于c语言中,没有直接的字典,字符串数组等数据结构,所以要借助结构体定义,处理json。如果有对应的数据结构就方便一些, 如python中用json.loads(json)就把json字符串转变为内建的数据结构处理起来比较方便。     cjson库文件下载:     sourceforge地址     一个重要概念:         在cjson中,js...