您目前尚未登陆,请选择【登陆】或【注册
首页->博客论坛->博客源代码(课程设计,3层架构)>>App-Code/PhotoManager.cs>>源码在线查看
温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:博客源代码(课程设计,3层架构)
当前文件:文件类型 MVCBlog/App_Code/PhotoManager.cs打开代码结构图
普通视图
		            
1using System; 2using System.Collections; 3using System.Collections.Generic; 4using System.Configuration; 5using System.Data; 6using System.Data.SqlClient; 7using System.Drawing; 8using System.Drawing.Drawing2D; 9using System.Drawing.Imaging; 10using System.IO; 11using System.Web; 12 13public class PhotoManager { 14 15 // 与照片相关的方法 16 //获取照片! 17 public static Stream GetPhoto(int photoid, PhotoSize size) 18 { 19 using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) 20 { 21 using (SqlCommand command = new SqlCommand("GetPhoto", connection)) 22 { 23 command.CommandType = CommandType.StoredProcedure; 24 command.Parameters.Add(new SqlParameter("@PhotoID", photoid)); 25 command.Parameters.Add(new SqlParameter("@Size", (int)size)); 26 connection.Open(); 27 object result = command.ExecuteScalar(); 28 try { 29 return new MemoryStream((byte[])result); 30 } 31 catch 32 { 33 return null; 34 } 35 } 36 } 37 } 38 //如果没有的话,用作图片占位符 39 public static Stream GetPhoto(PhotoSize size) { 40 string path = HttpContext.Current.Server.MapPath("~/Images/"); 41 switch (size) { 42 case PhotoSize.Small: 43 path += "placeholder-100.jpg"; 44 break; 45 case PhotoSize.Medium: 46 path += "placeholder-200.jpg"; 47 break; 48 case PhotoSize.Large: 49 path += "placeholder-600.jpg"; 50 break; 51 default: 52 path += "placeholder-600.jpg"; 53 break; 54 } 55 return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); 56 } 57 58 public static Stream GetFirstPhoto(int albumid, PhotoSize size) 59 { 60 using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) 61 { 62 using (SqlCommand command = new SqlCommand("GetFirstPhoto", connection)) 63 { 64 command.CommandType = CommandType.StoredProcedure; 65 command.Parameters.Add(new SqlParameter("@AlbumID", albumid)); 66 command.Parameters.Add(new SqlParameter("@Size", (int)size)); 67 68 69 connection.Open(); 70 object result = command.ExecuteScalar(); 71 try { 72 return new MemoryStream((byte[])result); 73 } catch { 74 return null; 75 } 76 } 77 } 78 } 79 80 public static List<Photo> GetPhotos(int AlbumID) { 81 using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { 82 using (SqlCommand command = new SqlCommand("GetPhotos", connection)) { 83 command.CommandType = CommandType.StoredProcedure; 84 command.Parameters.Add(new SqlParameter("@AlbumID", AlbumID)); 85 86 connection.Open(); 87 List<Photo> list = new List<Photo>(); 88 using (SqlDataReader reader = command.ExecuteReader()) { 89 while (reader.Read()) { 90 Photo temp = new Photo( 91 (int)reader["PhotoID"], 92 (int)reader["AlbumID"], 93 (string)reader["Caption"]); 94 list.Add(temp); 95 } 96 } 97 return list; 98 } 99 } 100 101 102 } 103 104 public static List<Photo> GetPhotos() { 105 return GetPhotos(GetRandomAlbumID()); 106 } 107 108 public static void AddPhoto(int AlbumID, string Caption, byte[] BytesOriginal) { 109 using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { 110 using (SqlCommand command = new SqlCommand("AddPhoto", connection)) { 111 command.CommandType = CommandType.StoredProcedure; 112 command.Parameters.Add(new SqlParameter("@AlbumID", AlbumID)); 113 command.Parameters.Add(new SqlParameter("@Caption", Caption)); 114 command.Parameters.Add(new SqlParameter("@BytesOriginal", BytesOriginal)); 115 command.Parameters.Add(new SqlParameter("@BytesFull", ResizeImageFile(BytesOriginal, 600))); 116 command.Parameters.Add(new SqlParameter("@BytesPoster", ResizeImageFile(BytesOriginal, 198))); 117 command.Parameters.Add(new SqlParameter("@BytesThumb", ResizeImageFile(BytesOriginal, 100))); 118 connection.Open(); 119 command.ExecuteNonQuery(); 120 } 121 } 122 } 123 124 public static void RemovePhoto(int PhotoID) { 125 using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { 126 using (SqlCommand command = new SqlCommand("RemovePhoto", connection)) { 127 command.CommandType = CommandType.StoredProcedure; 128 command.Parameters.Add(new SqlParameter("@PhotoID", PhotoID)); 129 connection.Open(); 130 command.ExecuteNonQuery(); 131 } 132 } 133 } 134 135 public static void EditPhoto(string Caption, int PhotoID) { 136 using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { 137 using (SqlCommand command = new SqlCommand("EditPhoto", connection)) { 138 command.CommandType = CommandType.StoredProcedure; 139 command.Parameters.Add(new SqlParameter("@Caption", Caption)); 140 command.Parameters.Add(new SqlParameter("@PhotoID", PhotoID)); 141 connection.Open(); 142 command.ExecuteNonQuery(); 143 } 144 } 145 } 146 147 // 与相册相关的方法 148 149 public static List<Album> GetAlbums() 150 { 151 using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { 152 using (SqlCommand command = new SqlCommand("GetAlbums", connection)) { 153 command.CommandType = CommandType.StoredProcedure; 154 155 connection.Open(); 156 List<Album> list = new List<Album>(); 157 using (SqlDataReader reader = command.ExecuteReader()) { 158 while (reader.Read()) { 159 Album temp = new Album( 160 (int)reader["AlbumID"], 161 (int)reader["NumberOfPhotos"], 162 (string)reader["Caption"]); 163 list.Add(temp); 164 } 165 } 166 return list; 167 } 168 } 169 } 170 171 public static void AddAlbum(string Caption) { 172 using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { 173 using (SqlCommand command = new SqlCommand("AddAlbum", connection)) { 174 command.CommandType = CommandType.StoredProcedure; 175 command.Parameters.Add(new SqlParameter("@Caption", Caption)); 176 connection.Open(); 177 command.ExecuteNonQuery(); 178 } 179 } 180 } 181 182 public static void RemoveAlbum(int AlbumID) { 183 using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { 184 using (SqlCommand command = new SqlCommand("RemoveAlbum", connection)) { 185 command.CommandType = CommandType.StoredProcedure; 186 command.Parameters.Add(new SqlParameter("@AlbumID", AlbumID)); 187 connection.Open(); 188 command.ExecuteNonQuery(); 189 } 190 } 191 } 192 193 public static void EditAlbum(string Caption, int AlbumID) { 194 using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { 195 using (SqlCommand command = new SqlCommand("EditAlbum", connection)) { 196 command.CommandType = CommandType.StoredProcedure; 197 command.Parameters.Add(new SqlParameter("@Caption", Caption)); 198 command.Parameters.Add(new SqlParameter("@AlbumID", AlbumID)); 199 connection.Open(); 200 command.ExecuteNonQuery(); 201 } 202 } 203 } 204 205 public static int GetRandomAlbumID() { 206 using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) { 207 using (SqlCommand command = new SqlCommand("GetNonEmptyAlbums", connection)) { 208 command.CommandType = CommandType.StoredProcedure; 209 connection.Open(); 210 List<Album> list = new List<Album>(); 211 using (SqlDataReader reader = command.ExecuteReader()) { 212 while (reader.Read()) { 213 Album temp = new Album((int)reader["AlbumID"], 0, ""); 214 list.Add(temp); 215 } 216 } 217 try { 218 Random r = new Random(); 219 return list[r.Next(list.Count)].AlbumID; 220 } catch { 221 return -1; 222 } 223 } 224 } 225 } 226 227 // Helper 函数 228 229 private static byte[] ResizeImageFile(byte[] imageFile, int targetSize) { 230 using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile))) { 231 Size newSize = CalculateDimensions(oldImage.Size, targetSize); 232 using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb)) { 233 using (Graphics canvas = Graphics.FromImage(newImage)) { 234 canvas.SmoothingMode = SmoothingMode.AntiAlias; 235 canvas.InterpolationMode = InterpolationMode.HighQualityBicubic; 236 canvas.PixelOffsetMode = PixelOffsetMode.HighQuality; 237 canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize)); 238 MemoryStream m = new MemoryStream(); 239