科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道在ASP.NET中上传图片并生成缩略图的C#源码(二)

在ASP.NET中上传图片并生成缩略图的C#源码(二)

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

   public void UploadFile(object sender, System.EventArgs e) { string imgNameOnly, imgNameNoExt, imgExt;string im

作者:中国IT实验室 来源:中国IT实验室 2007年9月12日

关键字: ASP.NET C# 编程

  • 评论
  • 分享微博
  • 分享邮件
  

    public void UploadFile(object sender, System.EventArgs e)

    {

    string imgNameOnly, imgNameNoExt, imgExt;string imgThumbnail;int erroNumber = 0;System.Drawing.Image oriImg, newImg;string strFePicSavePath = ConfigurationSettings.AppSettings["FePicSavePath"].ToString();string strFePicThumbFormat = ConfigurationSettings.AppSettings["FePicThumbFormat"].ToString()。ToLower();int intFeThumbWidth = Int32.Parse(ConfigurationSettings.AppSettings["FePicThumbWidth"]);string fileExt;

    StringBuilder picInfo = new StringBuilder();

    if(Page.IsValid)

    {

    for(int i = 0;i < Request.Files.Count; i++)

    { HttpPostedFile PostedFile = Request.Files[i];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) && (checkboxlistRewrite.Items[i].Selected == false))

    { erroNumber = erroNumber + 1;picInfo.Append("<b>错误:</b>文件("+ (i+1) +") " + imgNameOnly + " 已经存在,请修改文件名<br/>" );} else { erroNumber = erroNumber + 1;picInfo.Append("<b>错误:</b>文件("+ (i+1) +") " + imgNameOnly + " 扩展名 " + fileExt + " 不被许可<br/>" );}

    }

    if(erroNumber > 0)

    { picInfo.Append("<font color=red>全部操作均未完成,请修改错误,再进行操作</font><br/>");} else { for(int i = 0;i < Request.Files.Count; i++)

    {

    HttpPostedFile PostedFile = Request.Files[i];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(intFeThumbWidth, intFeThumbWidth * oriImg.Height/oriImg.Width,null,new System.IntPtr(0));switch(imgExt)

    { //case ".jpeg":case ".jpg":oriImg.Save(strFePicSavePath + imgNameOnly , System.Drawing.Imaging.ImageFormat.Jpeg);break;case ".gif":oriImg.Save(strFePicSavePath + imgNameOnly , System.Drawing.Imaging.ImageFormat.Gif);break;case ".png":oriImg.Save(strFePicSavePath + imgNameOnly , System.Drawing.Imaging.ImageFormat.Png);break;}

    //oriImg.Save(ConfigurationSettings.AppSettings["FePicSavePath"] + imgNameNoExt + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

    switch(strFePicThumbFormat)

    { //jpeg format can get the smallest file size, and the png is the largest size //case "jpeg":case "jpg":newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);imgThumbnail = imgNameOnly + "_thumb.jpg";break;case "gif":newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.gif",System.Drawing.Imaging.ImageFormat.Gif);imgThumbnail = imgNameOnly + "_thumb.gif";break;case "png":newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.png",System.Drawing.Imaging.ImageFormat.Png);imgThumbnail = imgNameOnly + "_thumb.png";break;default:newImg.Save(strFePicSavePath + imgNameOnly + "_thumb.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);imgThumbnail = imgNameOnly + "_thumb.jpg";break;

    }//switch

    picInfo.Append("<b>文件 名:</b>" + imgNameOnly + " ( " + oriImg.Width + " x " + oriImg.Height + " ) " + PostedFile.ContentLength/1024 + "KB<br/>");picInfo.Append("<b>缩略图名:</b>" + imgThumbnail + " ( " + newImg.Width + " x " + newImg.Height + " )<br/><br/>");

    oriImg.Dispose();newImg.Dispose();

    }//for picInfo.Append("<font color=red>所有操作成功</font><br/>");

    }// if erronumber = 0

    } else { picInfo.Append("<font color=red>有错误,请检查。操作未成功</font><br/>");

    }

    lblPicInfo.Text = picInfo.ToString();

    } </FONT>

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章