最精简的相册管理代码2010-09-26Micrsoft为我们提供了最精简的相册管理代码:using System; using System.Collections; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using System.Web;
//移除一张图片(指定图片ID) public static void RemovePhoto(int PhotoID) { using (SqlConnection connection = new SqlConnection(ConnString)) { using (SqlCommand command = new SqlCommand("RemovePhoto", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.Add(new SqlParameter("@PhotoID", PhotoID)); connection.Open(); command.ExecuteNonQuery(); } } }
//编辑图片(编辑后的标题与所编辑的图片ID) public static void EditPhoto(string Caption, int PhotoID) { using (SqlConnection connection = new SqlConnection(ConnString)) { using (SqlCommand command = new SqlCommand("EditPhoto", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.Add(new SqlParameter("@Caption", Caption)); command.Parameters.Add(new SqlParameter("@PhotoID", PhotoID)); connection.Open(); command.ExecuteNonQuery(); } } }
// 获取相册(在相册页面中调用) public static List<Album> GetAlbums() { using (SqlConnection connection = new SqlConnection(ConnString)) { using (SqlCommand command = new SqlCommand("GetAlbums", connection)) { command.CommandType = CommandType.StoredProcedure; bool filter = !(HttpContext.Current.User.IsInRole("Friends") || HttpContext.Current.User.IsInRole("Administrators")); command.Parameters.Add(new SqlParameter("@IsPublic", filter)); connection.Open(); List<Album> list = new List<Album>(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { Album temp = new Album( (int)reader["AlbumID"], (int)reader["NumberOfPhotos"], (string)reader["Caption"], (bool)reader["IsPublic"]); list.Add(temp); } } return list; } } }
//添加新相册(相册名与是否公开属性) public static void AddAlbum(string Caption, bool IsPublic) { using (SqlConnection connection = new SqlConnection(ConnString)) { using (SqlCommand command = new SqlCommand("AddAlbum", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.Add(new SqlParameter("@Caption", Caption)); command.Parameters.Add(new SqlParameter("@IsPublic", IsPublic)); connection.Open(); command.ExecuteNonQuery(); } } }
//删除一个相册(删除一个相册时里面的所有照片也同时删除) public static void RemoveAlbum(int AlbumID) { using (SqlConnection connection = new SqlConnection(ConnString)) { using (SqlCommand command = new SqlCommand("RemoveAlbum", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.Add(new SqlParameter("@AlbumID", AlbumID)); connection.Open(); command.ExecuteNonQuery(); } } }
//编辑相册(编辑标量与公开属性,指定相册ID号) public static void EditAlbum(string Caption, bool IsPublic, int AlbumID) { using (SqlConnection connection = new SqlConnection(ConnString)) { using (SqlCommand command = new SqlCommand("EditAlbum", connection)) { command.CommandType = CommandType.StoredProcedure; command.Parameters.Add(new SqlParameter("@Caption", Caption)); command.Parameters.Add(new SqlParameter("@IsPublic", IsPublic)); command.Parameters.Add(new SqlParameter("@AlbumID", AlbumID)); connection.Open(); command.ExecuteNonQuery(); } } }
//获取随机相册号(1-最大相册号之间) public static int GetRandomAlbumID() { using (SqlConnection connection = new SqlConnection(ConnString)) { using (SqlCommand command = new SqlCommand("GetNonEmptyAlbums", connection)) { command.CommandType = CommandType.StoredProcedure; connection.Open(); List<Album> list = new List<Album>(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { Album temp = new Album((int)reader["AlbumID"], 0, "", false); list.Add(temp); } } try { Random r = new Random(); return list[r.Next(list.Count)].AlbumID; } catch { return -1; } } } }
//指定图片大小,根据自定义而缩放图片 private static byte[] ResizeImageFile(byte[] imageFile, int targetSize) { using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile))) { Size newSize = CalculateDimensions(oldImage.Size, targetSize); using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb)) { using (Graphics canvas = Graphics.FromImage(newImage)) { canvas.SmoothingMode = SmoothingMode.AntiAlias; canvas.InterpolationMode = InterpolationMode.HighQualityBicubic; canvas.PixelOffsetMode = PixelOffsetMode.HighQuality; canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize)); MemoryStream m = new MemoryStream(); newImage.Save(m, ImageFormat.Jpeg); return m.GetBuffer(); } } } }