zoukankan      html  css  js  c++  java
  • 图片上传,并自动生成缩略图!

    代码如下:
      /// <summary>
      /// 上传单个图片。生成缩略图,jpg格式。
      /// 调用方法:protected System.Web.UI.HtmlControls.HtmlInputFile FilePath;
      /// 类名.uploadPhoto(FilePath.PostedFile.InputStream);  
      /// </summary>
      /// <param name="source"></param>
     
      public static string  uploadPhoto(System.IO.Stream source)
      {   
       string photoGuid=System.Guid.NewGuid().ToString();
       //string sourcePic=System.Web.HttpContext.Current.Server.MapPath("/Upload/Photo/" + photoGuid + ".jpg");
       string path =System.Web.HttpContext.Current.Server.MapPath("/");
       string sourcePic= path + System.Configuration.ConfigurationSettings.AppSettings["photoPath"] +   photoGuid + ".jpg";  
       string thumbPic=path +System.Configuration.ConfigurationSettings.AppSettings["thumbPath"] +  photoGuid + ".jpg";
    //   int thumbWidth=120;
    //   int thumbHeight=160;
       int thumbWidth=114;
       int thumbHeight=154;
     
       //生成图片id
       Image image = Image.FromStream(source);
       if(image.Height>600 || image.Width >600)
        Compress(image,sourcePic);
       else
       {
        FileStream srcfs = new FileStream(sourcePic,FileMode.Create);
        image.Save(srcfs, ImageFormat.Jpeg);
        srcfs.Close();
       }
     
       //生成缩略图
       int tw = thumbWidth;
       int th = thumbHeight;
       if(image.Height / th > image.Width / tw)
       {
        tw = image.Width * th / image.Height;
       }
       else
       {
        th = image.Height * tw / image.Width;
       }
       Image thumbImage = Image.FromHbitmap(new Bitmap(thumbWidth,thumbHeight,PixelFormat.Format32bppRgb).GetHbitmap());
       Graphics g = Graphics.FromImage(thumbImage);
       SolidBrush brush = new SolidBrush(Color.White);
       g.FillRectangle(brush, 0, 0, thumbWidth, thumbHeight);
       g.DrawImage(image, (thumbWidth - tw)/2, (thumbHeight -th)/2, tw, th);
       g.Dispose();
       FileStream thumbfs= new FileStream(thumbPic,FileMode.Create);
       thumbImage.Save(thumbfs,ImageFormat.Jpeg);
       
       thumbfs.Close();
       image.Dispose();   
       return(photoGuid);
      }
      public static void Compress(Image sourceImage, string filename)
      {
       int targetWidth = 600;
       int targetHeight = 600;
     
       if(sourceImage.Height <= 600 && sourceImage.Width <= 600)
       {
        targetHeight = sourceImage.Height;
        targetWidth = sourceImage.Width;
       }
       else if(sourceImage.Height > sourceImage.Width)
       {
        targetWidth = sourceImage.Width * 600 / sourceImage.Height;
       }
       else
       {
        targetHeight = sourceImage.Height * 600 / sourceImage.Width;
       }
     
       // 缩放图片
       Image targetImage = Image.FromHbitmap(new Bitmap(targetWidth, targetHeight, PixelFormat.Format32bppRgb).GetHbitmap());
       Graphics g = Graphics.FromImage(targetImage);
       g.DrawImage(sourceImage, 0, 0, targetWidth, targetHeight);
       g.Dispose();
       // 设置输出格式
       EncoderParameters encParams = new EncoderParameters(1);
       encParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L);
       ImageCodecInfo codeInfo = null;
       ImageCodecInfo[] codeInfos = ImageCodecInfo.GetImageEncoders();
       foreach(ImageCodecInfo info in codeInfos)
       {
        if(info.MimeType.Equals("image/jpeg"))
        {
         codeInfo = info;
         break;
        }
       }
       if(codeInfo != null)
       {
        targetImage.Save(filename, codeInfo, encParams);
       }
       targetImage.Dispose();
       
      }


    Web.Config中的设置如下:
     <appSettings>
      <add key="photoPath" value="\upload\photo\"/>
     <add key="thumbPath" value="\upload\thumb\"/>
     <add key="otherFilePath" value="\upload\other\"/>
     </appSettings>
  • 相关阅读:
    JdbcTemplate
    C# 将内存中的datatable数据导出为Excel(方法一,以文件流方式导出)【转载】
    DEV GridControl小结【转载】
    DEV控件:gridControl常用属性设置2【转载】
    DevExpress控件学习总结【转载】
    DEV中的TreeList控件应用的一个小效果实现【转载】
    DEV控件:gridControl常用属性设置【转载】
    工具箱修复Dev控件显示【转载】
    C#如何实现记住密码,自动登录功能?【转载】
    将MenuStrip控件中的信息添加到TreeView控件中【转载】
  • 原文地址:https://www.cnblogs.com/hxling/p/371559.html
Copyright © 2011-2022 走看看