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

有关上传图片缩略图保存到文件夹得问题

2013年11月19日 ⁄ 综合 ⁄ 共 2272字 ⁄ 字号 评论关闭

protected void Button1_Click(object sender, EventArgs e)
        {
            string imgNameOnly, imgNameNoExt, imgExt;
            string fileExt;
            System.Drawing.Image oriImg, newImg;//定义图片
            string strFePicSavePath = "UpLoad/Head_Photo/";
            HttpPostedFile PostedFile = this.FileUpload1.PostedFile;//HttpPostedFile对象,用于读取图象文件属性
            int iFileLength = PostedFile.ContentLength;//记录文件长度
            if (iFileLength == 0)
            {
                this.Label2.Text = "请选择要上传的文件!";
            }
            else
            {
                fileExt = (System.IO.Path.GetExtension(PostedFile.FileName)).ToString().ToLower();
                imgNameOnly = System.IO.Path.GetFileName(PostedFile.FileName);
                if (fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".png")
                {
                    if (System.IO.File.Exists(strFePicSavePath + imgNameOnly))
                    {
                        Response.Write(" <script>alert('此文件名已存在!请将照片重命名后再上传!');history.back(); </script>");
                        return;
                    }
                    else
                    {
                        imgNameOnly = System.IO.Path.GetFileName(PostedFile.FileName);
                        imgNameNoExt = System.IO.Path.GetFileNameWithoutExtension(PostedFile.FileName);
                        imgExt = System.IO.Path.GetExtension(PostedFile.FileName).ToString().ToLower();
                        oriImg = System.Drawing.Image.FromStream(PostedFile.InputStream);
                        newImg = oriImg.GetThumbnailImage(122, 122 * oriImg.Height / oriImg.Width, null, new System.IntPtr(0));
                        switch (imgExt)
                        {
                           
                            case ".jpg":
                              newImg.Save(strFePicSavePath + imgNameNoExt + "_thumb.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);//“GDI+ 中发生一般性错误。” 

/*改为newImg.Save(Server.MapPath(strFePicSavePath + imgNameNoExt + "_thumb.jpg", System.Drawing.Imaging.ImageFormat.Jpeg));*/
                                break;
                            case ".gif":
                                oriImg.Save(strFePicSavePath + imgNameNoExt + "_thumb.jpg", System.Drawing.Imaging.ImageFormat.Gif);
                                break;
                            case ".png":
                                oriImg.Save(strFePicSavePath + imgNameNoExt + "_thumb.jpg", System.Drawing.Imaging.ImageFormat.Png);
                                break;
                         
                        }
                        oriImg.Dispose();
                        newImg.Dispose();
                    }
                }
                else
                {
                    Response.Write(" <script>alert('只能上传.jpg,.bmp,.gif,.png格式的图片!');history.back(); </script>");
                    return;
                }

            }
        }

抱歉!评论已关闭.