图片上传生成缩略图

今天在做一个关于手机端html5图片批量上传从手机能读取相册和照相机,但是网上好多例子是针对于php进行开发的。貌似html5也不能支持flash进行多选等等。所以就在网上找了例子纯js的,刚开始看的时候一头雾水根本看不清楚,只能自己默默看,不过最好还好能显示出来了。网上的例子在我本机上面运行是可以的,但是在手机上测试的时候怎么会有那么多奇葩的反应,但是只有手机谷歌浏览器是最完美的。由于手机上截图不好只能在电脑上截图。



其实我不是想说什么。关键我是想说在最后面,关于处理程程序上传这一块。

context.Response.ContentType = "text/plain";
        HttpFileCollection file = context.Request.Files;
        try
        {
            for (int i = 0; i < file.Count; i++)
            {
                postfile = file[i];
                imgName = DateTime.Now.ToString("yyyyMMddhhmmss");
                imgPath = "~/UploadImages/upload/" + imgName + postfile.FileName;   //通过此对象获取文件名
                smimg = "~/UploadImages/upload/smupload/" + "sml" + imgName + postfile.FileName;
                postfile.SaveAs(context.Server.MapPath(imgPath));
                simg.GetThumbnail(context.Server.MapPath(imgPath), context.Server.MapPath(smimg), 60, 60);

            }
            context.Response.Write("图片上传成功!");
        }
        catch
        {
            context.Response.Write("图片上传失败!");
        }
刚开始的时候针对路径这一块很坑爹,导致我根本看不出来是什么问题,报出什么什么没权限什么的,什么IIS权限不足什么的,配置ASP。NET账户什么等等,导致我百度那么长时间尽然每一个能解决的。最后我发现根本不是那个问题,可能是我遇到的不是那种情况
imgPath = "~/UploadImages/upload/" + imgName + postfile.FileName;上传的时候要写上图片的类型如“.jpeg”等等
顺便上传一下缩略图的code
<pre name="code" class="csharp"> /// <summary>
        /// 为图片生成缩略图
        /// </summary>
        /// <param name="phyPath">原图片的路径</param>        /// 
        /// <param name="phyPath">缩略图的路径</param>
        /// <param name="width">缩略图宽</param>
        /// <param name="height">缩略图高</param>
        /// <returns></returns>
        public string GetThumbnail(string phyPath, string sSavePath, int width, int height)
        {
            System.Drawing.Image image = System.Drawing.Image.FromFile(phyPath);
            int orignWidth = image.Width;	//原图尺寸
            int orignHeight = image.Height;
            float toWidth = (float)width;	//缩略图目标尺寸
            float toHeight = (float)height;
            float radio = (float)orignWidth / (float)orignHeight;		//宽高比例
            if (orignWidth >= orignHeight)
            {		//宽度不变
                toHeight = toWidth / radio;
            }
            else
            {		//高度不变
                toWidth = toHeight * radio;
            }


            System.Drawing.Image thumbImage = image.GetThumbnailImage(Convert.ToInt32(toWidth), Convert.ToInt32(toHeight), null, IntPtr.Zero);
            //string folder = Path.GetDirectoryName(phyPath);
            //string fileName = "\\" + Path.GetFileName(phyPath);
            //string savePath = folder + fileName;
            try
            {
                thumbImage.Save(sSavePath, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            finally
            {
                image.Dispose();
                thumbImage.Dispose();
            }
            return sSavePath;
        }


 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值