C# webform上传图片并生成缩略图
其实里面写的很乱,包括修改文件名什么的都没有仔细去写,主要是想记录下缩略图生成的几种方式 ,大家明白就好!
1 void UpImgs() 2 { 3 if (FileUpload1.HasFile) 4 { 5 string fileContentType = FileUpload1.PostedFile.ContentType; 6 if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg") 7 { 8 string name = FileUpload1.PostedFile.FileName; // 客户端文件路径 9 10 FileInfo file = new FileInfo(name); 11 //string fileName = CreatFileName(file.Name)+"_"+file.Name; // 文件名称 12 //string fileName_s = "s_" + CreatFileName(file.Name) + "_" + file.Name; // 缩略图文件名称 13 14 string fileName = CreatFileName(file.Name); // 文件名称 15 string fileName_s = "s_" + CreatFileName(file.Name); // 缩略图文件名称 16 17 18 string webFilePath = Server.MapPath("../file/" + fileName); // 服务器端文件路径 19 string webFilePath_s = Server.MapPath("../file/" + fileName_s); // 服务器端缩略图路径 20 21 22 string dbname = "~/file/" + fileName;//存放到<a href="http://lib.csdn.net/base/mysql" class='replace_word' title="MySQL知识库" target='_blank' style='color:#df3434; font-weight:bold;'>数据库</a>里的相对路径 23 string dbnames = "~/file/" + fileName_s;//存放到数据库里的相对路径 24 25 if (!File.Exists(webFilePath)) 26 { 27 try 28 { 29 FileUpload1.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件 30 MakeThumbnail(webFilePath, webFilePath_s, 200, 160, "Cut"); // 生成缩略图方法 31 //Label1.Text = "提示:文件“" + fileName + "”成功上传,并生成“" + fileName_s + "”缩略图,文件类型为:" + FileUpload1.PostedFile.ContentType + ",文件大小为:" + FileUpload1.PostedFile.ContentLength + "B"; 32 Label1.Text = "提示:文件“" + dbname + "”成功上传,并生成“" + dbnames + "”缩略图,文件类型为:" + FileUpload1.PostedFile.ContentType + ",文件大小为:" + FileUpload1.PostedFile.ContentLength + "B"; 33 34 35 File.Delete(webFilePath);//删除源文件,在这我把源文件删了,只保留了缩小后的图片 36 37 38 39 } 40 catch (Exception ex) 41 { 42 Label1.Text = "提示:文件上传失败,失败原因:" + ex.Message; 43 44 } 45 } 46 else 47 { 48 Label1.Text = "提示:文件已经存在,请重命名后上传"; 49 50 } 51 } 52 else 53 { 54 Label1.Text = "提示:文件类型不符"; 55 56 } 57 } 58 } 59 /**//// 60 /// 生成缩略图 61 /// 62 /// 源图路径(物理路径) 63 /// 缩略图路径(物理路径) 64 /// 缩略图宽度 65 /// 缩略图高度 66 /// 生成缩略图的方式 67 public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode) 68 { 69 System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); 70 71 int towidth = width; 72 int toheight = height; 73 74 int x = 0; 75 int y = 0; 76 int ow = originalImage.Width; 77 int oh = originalImage.Height; 78 79 switch (mode) 80 { 81 case "HW"://指定高宽缩放(可能变形) 82 break; 83 case "W"://指定宽,高按比例 84 toheight = originalImage.Height * width / originalImage.Width; 85 break; 86 case "H"://指定高,宽按比例 87 towidth = originalImage.Width * height / originalImage.Height; 88 break; 89 case "Cut"://指定高宽裁减(不变形) 90 if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) 91 { 92 oh = originalImage.Height; 93 ow = originalImage.Height * towidth / toheight; 94 y = 0; 95 x = (originalImage.Width - ow) / 2; 96 } 97 else 98 { 99 ow = originalImage.Width; 100 oh = originalImage.Width * height / towidth; 101 x = 0; 102 y = (originalImage.Height - oh) / 2; 103 } 104 break; 105 default: 106 break; 107 } 108 109 //新建一个bmp图片 110 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); 111 112 //新建一个画板 113 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); 114 115 //设置高质量插值法 116 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 117 118 //设置高质量,低速度呈现平滑程度 119 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 120 121 //清空画布并以透明背景色填充 122 g.Clear(System.Drawing.Color.Transparent); 123 124 //在指定位置并且按指定大小绘制原图片的指定部分 125 g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), 126 new System.Drawing.Rectangle(x, y, ow, oh), 127 System.Drawing.GraphicsUnit.Pixel); 128 129 try 130 { 131 //以jpg格式保存缩略图 132 bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); 133 } 134 catch (System.Exception e) 135 { 136 throw e; 137 } 138 finally 139 { 140 originalImage.Dispose(); 141 bitmap.Dispose(); 142 g.Dispose(); 143 } 144 } 145 146 string CreatFileName(string fileName) 147 { 148 149 DateTime now = DateTime.Now; 150 151 string str = 152 153 now.Year.ToString() + 154 155 now.Month.ToString() + 156 157 now.Day.ToString() + 158 159 now.Hour.ToString() + 160 161 now.Minute.ToString() + 162 163 now.Second.ToString() + 164 165 fileName; 166 167 return str; 168 169 }