FileUpload上传并压缩图片的几种方法(方法转)

  1     private bool UploadImg(FileUpload FileUpload, string FilePath, string imagess)
  2         {
  3             //string fileExtends = "";
  4             if (FileUpload.FileName.Trim().Length > 0)
  5             {
  6                 string fileExt = System.IO.Path.GetExtension(FileUpload.FileName);
  7                 if (fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".png" || fileExt == "ico")
  8                 {
  9                     try
 10                     {
 11                         if (FileUpload.PostedFile.ContentLength < 1048576)
 12                         {
 13 
 14                             //FileInfo fileinfo = new FileInfo(FileUpload.PostedFile.FileName);
 15                             //FileUpload.SaveAs(Server.MapPath(FilePath + imagess));
 16                             System.Drawing.Image image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(FileUpload.FileBytes));
 17                             //if (GetPicThumbnail(image, FilePath + imagess, img1.Height, img1.Width, 100))
 18                             //{
 19                             //    return true;
 20                             //}
 21                             //else
 22                             //{
 23                             //    return false;
 24                             //}
 25                             if (MakeThumbnail(image, Server.MapPath(FilePath + imagess), img1.Width, img1.Height, "Cut", "JPG"))
 26                             {
 27                                 return true;
 28                             }
 29                             else
 30                             {
 31                                 return false;
 32                             }
 33                             //FileUpload.SaveAs(Server.MapPath("UiMain\Employment\images\" + imagess));
 34 
 35                         }
 36                         else
 37                         {
 38                             Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script type="text/javascript">  new function(){alert('上传图片大小不能超过1MB');}</script>");
 39                             return false;
 40                         }
 41                     }
 42                     catch (Exception ex)
 43                     {
 44                         Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script type="text/javascript">  new function(){alert('发生错误'" + ex + "');}</script>");
 45                         return false;
 46                     }
 47                 }
 48                 else
 49                 {
 50                     Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script type="text/javascript"> new function(){alert('只允许上传jpg、gif、png、ico.文件!')};</script>");
 51                     return false;
 52                 }
 53             }
 54             else
 55             {
 56                 return false;
 57             }
 58         }
 59 
 60 
 61         /// <summary>
 62         /// 压缩图片并存储
 63         /// </summary>
 64         /// <param name="originalImagePath">源图路径(物理路径)</param>
 65         /// <param name="thumbnailPath">压缩面存储路径(物理路径)</param>
 66         /// <param name="width">压缩图宽度</param>
 67         /// <param name="height">压缩图高度</param>
 68         /// <param name="mode">生成压缩图的方式</param> 
 69         /// <param name="type">保存压缩图类型</param> 
 70         public static bool MakeThumbnail(System.Drawing.Image originalImagePath, string thumbnailPath, int width, int height, string mode, string type)
 71         {
 72             //System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
 73             System.Drawing.Image originalImage = originalImagePath;//System.Drawing.Image.FromFile(originalImagePath);
 74             bool isture = true; ;
 75             int towidth = width;
 76             int toheight = height;
 77             int x = 0;
 78             int y = 0;
 79             int ow = originalImage.Width;
 80             int oh = originalImage.Height;
 81             switch (mode)
 82             {
 83                 case "HW"://指定高宽缩放(可能变形) 
 84                     break;
 85                 case "W"://指定宽,高按比例 
 86                     toheight = originalImage.Height * width / originalImage.Width;
 87                     break;
 88                 case "H"://指定高,宽按比例
 89                     towidth = originalImage.Width * height / originalImage.Height;
 90                     break;
 91                 case "Cut"://指定高宽裁减(不变形) 
 92                     if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
 93                     {
 94                         oh = originalImage.Height;
 95                         ow = originalImage.Height * towidth / toheight;
 96                         y = 0;
 97                         x = (originalImage.Width - ow) / 2;
 98                     }
 99                     else
100                     {
101                         ow = originalImage.Width;
102                         oh = originalImage.Width * height / towidth;
103                         x = 0;
104                         y = (originalImage.Height - oh) / 2;
105                     }
106                     break;
107                 case "DB"://等比缩放(不变形,如果高大按高,宽大按宽缩放) 
108                     if ((double)originalImage.Width / (double)towidth < (double)originalImage.Height / (double)toheight)
109                     {
110                         toheight = height;
111                         towidth = originalImage.Width * height / originalImage.Height;
112                     }
113                     else
114                     {
115                         towidth = width;
116                         toheight = originalImage.Height * width / originalImage.Width;
117                     }
118                     break;
119                 default:
120                     break;
121             }
122             //新建一个bmp图片
123             System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
124             //新建一个画板
125             System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
126             //设置高质量插值法
127             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
128             //设置高质量,低速度呈现平滑程度
129             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
130             //清空画布并以透明背景色填充
131             g.Clear(System.Drawing.Color.Transparent);
132             //在指定位置并且按指定大小绘制原图片的指定部分
133             g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
134             new System.Drawing.Rectangle(x, y, ow, oh),
135             System.Drawing.GraphicsUnit.Pixel);
136             try
137             {
138                 //保存缩略图
139                 if (type == "JPG")
140                 {
141                     bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
142                 }
143                 if (type == "BMP")
144                 {
145                     bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Bmp);
146                 }
147                 if (type == "GIF")
148                 {
149                     bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Gif);
150                 }
151                 if (type == "PNG")
152                 {
153                     bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Png);
154                 }
155             }
156             catch (System.Exception e)
157             {
158                 isture = false;
159             }
160             finally
161             {
162                 originalImage.Dispose();
163                 bitmap.Dispose();
164                 g.Dispose();
165             }
166             return isture;
167         }
168 
169 
170         /// <summary>
171         /// 无损压缩图片
172         /// </summary>
173         /// <param name="Image">原图片</param>
174         /// <param name="dFile">压缩后保存位置</param>
175         /// <param name="dHeight">高度</param>
176         /// <param name="dWidth"></param>
177         /// <param name="flag">压缩质量 1-100</param>
178         /// <returns></returns>
179         public bool GetPicThumbnail(System.Drawing.Image Image, string dFile, int dHeight, int dWidth, int flag)
180         {
181             System.Drawing.Image iSource = Image;
182             ImageFormat tFormat = iSource.RawFormat;
183             int sW = 0, sH = 0;
184             //按比例缩放
185             Size tem_size = new Size(iSource.Width, iSource.Height);
186             if (tem_size.Width > dHeight || tem_size.Width > dWidth) //将**改成c#中的或者操作符号
187             {
188                 if ((tem_size.Width * dHeight) > (tem_size.Height * dWidth))
189                 {
190                     sW = dWidth;
191                     sH = (dWidth * tem_size.Height) / tem_size.Width;
192                 }
193                 else
194                 {
195                     sH = dHeight;
196                     sW = (tem_size.Width * dHeight) / tem_size.Height;
197                 }
198             }
199             else
200             {
201                 sW = tem_size.Width;
202                 sH = tem_size.Height;
203             }
204             Bitmap ob = new Bitmap(dWidth, dHeight);
205             Graphics g = Graphics.FromImage(ob);
206             g.Clear(Color.WhiteSmoke);
207             g.CompositingQuality = CompositingQuality.HighQuality;
208             g.SmoothingMode = SmoothingMode.HighQuality;
209             g.InterpolationMode = InterpolationMode.HighQualityBicubic;
210             g.DrawImage(iSource, new Rectangle((dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
211             g.Dispose();
212             //以下代码为保存图片时,设置压缩质量
213             EncoderParameters ep = new EncoderParameters();
214             long[] qy = new long[1];
215             qy[0] = flag;//设置压缩的比例1-100
216             EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
217             ep.Param[0] = eParam;
218             try
219             {
220                 ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
221                 ImageCodecInfo jpegICIinfo = null;
222                 for (int x = 0; x < arrayICI.Length; x++)
223                 {
224                     if (arrayICI[x].FormatDescription.Equals("JPEG"))
225                     {
226                         jpegICIinfo = arrayICI[x];
227                         break;
228                     }
229                 }
230                 if (jpegICIinfo != null)
231                 {
232                     ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
233                 }
234                 else
235                 {
236                     ob.Save(dFile, tFormat);
237                 }
238                 return true;
239             }
240             catch (Exception ex)
241             {
242                 return false;
243             }
244             finally
245             {
246                 iSource.Dispose();
247                 ob.Dispose();
248             }
249         }
原文地址:https://www.cnblogs.com/felicitytanyin/p/3409805.html