189 8069 5689

c#怎么实现多图片上传并生成缩略图-创新互联

这篇文章主要介绍“c#怎么实现多图片上传并生成缩略图”,在日常操作中,相信很多人在c#怎么实现多图片上传并生成缩略图问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”c#怎么实现多图片上传并生成缩略图”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

创新互联专注于巴彦网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供巴彦营销型网站建设,巴彦网站制作、巴彦网页设计、巴彦网站官网定制、成都微信小程序服务,打造巴彦网络公司原创品牌,更为您提供巴彦网站排名全网营销落地服务。

前台代码:

复制代码 代码如下:



 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %>

 

 
 
    
    
    
    
    
 
 
    


        

                      

                    

  •                     
                    

  •                 

  •                     
                    

  •                 

  •                     
                    

  •                 

  •                     

  •                 

  •                     

                    

  •                 

  •                     
                    

  •             

        

    

 
 



前台就是几个控件和一个ValidImage方法。

后台代码:

复制代码 代码如下:



  protected void btnPostFile_ServerClick(object sender, EventArgs e)
     {
         string filePath = Server.MapPath("/uploadImg");
         const int size = 5242880;
         if (!Directory.Exists(filePath))
         {
             Directory.CreateDirectory(filePath);
         }
         if (Request.Files.Count > 0)
         {
             for (int i = 0; i < Request.Files.Count; i++)
             {
                 HttpPostedFile postFile = Request.Files[i];
                 string uploadFileID = string.Format("#upload{0}", i + 1);  //当前的上传控件ID,因为jquery要调用就加了#
                 string msg = null;                 //提示信息
                 if (postFile.FileName.Trim().Length <= 0)
                 {
                     continue;
                 }
                 if (postFile.ContentLength > size)
                 {
                     msg = "文件太大";
                     Page.ClientScript.RegisterStartupScript(GetType(), "", "ValidImage(" + uploadFileID + "," + msg + ")", true);//将提示信息发送到客户端
                     continue;
                 }
                 string savePath = Path.Combine(filePath, postFile.FileName);        //图片的保存地址
                 if (!File.Exists(savePath))
                 {
                     postFile.SaveAs(Path.Combine(filePath, postFile.FileName));     //如果文件不存在就保存
                 }
                 else
                 {
                     msg = "文件" + postFile.FileName + "已经存在";
                     Page.ClientScript.RegisterStartupScript(GetType(), "", "ValidImage(" + uploadFileID + "," + msg + ")", true);//将提示信息发送到客户端
                     continue;
                 }
                 if (IsImg(savePath))            //通过IsImg方法验证文件是否是图片,或者格式是否正确
                 {
                     SmallImg(postFile.InputStream, postFile.FileName);
                 }
                 else
                 {
                     msg = "只能上传JGP、PNG类型的图片,请检查文件格式是否正确";
                     Page.ClientScript.RegisterStartupScript(GetType(), "", "ValidImage(" + uploadFileID + "," + msg + ")", true);//将提示信息发送到客户端
                     File.Delete(savePath);  //如果不是图片就删除
                 }
             }
         }
     }


复制代码 代码如下:

  #region 验证上传文件的格式
     ///


     /// 验证上传文件是否是图片
     ///

     /// 文件的保存路径
     ///
     private bool IsImg(string FilePath)
     {
         using (FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read))
         {
             bool result = false;
             BinaryReader br = new BinaryReader(fs, System.Text.Encoding.UTF8);
             string strImg = "";
             byte buffer;
             try
             {
                 buffer = br.ReadByte();
                 strImg = buffer.ToString();
                 buffer = br.ReadByte();
                 strImg += buffer.ToString();
             }
             catch
             {
                 fs.Close();
                 br.Close();

             }
             if (strImg == "255216" || strImg == "13780")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar
             {
                 result = true;
             }
             return result;
         }
     }
     #endregion


复制代码 代码如下:



   #region 将图片生成缩略图
     ///


     /// 生成缩略图
     ///

     private void SmallImg(Stream oStream, string FileName)
     {
         using (System.Drawing.Image img = System.Drawing.Image.FromStream(oStream))
         {
             int newWidth = 100;
             int newHeight = 80;
             int oldWidth = img.Width;
             int oldHeight = img.Height;
             if (oldWidth > oldHeight)
             {
                 newHeight = (int)Math.Floor((double)oldHeight * (double)newWidth / (double)oldWidth);
             }
             else
             {
                 newWidth = (int)Math.Floor((double)oldWidth * (double)newHeight / (double)oldHeight);
             }
             using (Bitmap bmp = new Bitmap(newWidth, newHeight))
             {
                 using (Graphics g = Graphics.FromImage(bmp))
                 {
                     g.Clear(Color.Transparent);
                     g.InterpolationMode = InterpolationMode.High;
                     g.CompositingQuality = CompositingQuality.HighQuality;
                     g.SmoothingMode = SmoothingMode.HighQuality;
                     g.DrawImage(img, new Rectangle(0, 0, newWidth, newHeight), new Rectangle(0, 0, oldWidth, oldHeight), GraphicsUnit.Pixel);
                     string newFileName = Path.GetFileNameWithoutExtension(FileName) + "_small" + Path.GetExtension(FileName);   //缩略图名称
                     string filePath = Server.MapPath("/uploadImg/") + newFileName;
                     bmp.Save(filePath);
                 }
             }

         }
     }
     #endregion


到此,关于“c#怎么实现多图片上传并生成缩略图”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


分享题目:c#怎么实现多图片上传并生成缩略图-创新互联
分享地址:http://jkwzsj.com/article/dsdedj.html

其他资讯