Micrsoft為我們提供了最精簡的相冊管理代碼:
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;
public class PhotoManager
{
// 指定數據庫連接串
public static string ConnString = "server=****;database=****; uid=sa; pwd=****; pooling=true";
//讀取數據庫中的照片
public static Stream GetPhoto(int photoid, PhotoSize size)
{
using (SqlConnection connection = new SqlConnection(ConnString))
{
using (SqlCommand command = new SqlCommand("GetPhoto", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@PhotoID", photoid));
command.Parameters.Add(new SqlParameter("@Size", (int)size));
bool filter = !(HttpContext.Current.User.IsInRole("FrIEnds") || HttpContext.Current.User.IsInRole("Administrators"));
command.Parameters.Add(new SqlParameter("@IsPublic", filter));
connection.Open();
object result = command.ExecuteScalar();
try
{
return new MemoryStream((byte[])result);
}
catch
{
return null;
}
}
}
}
//獲取默認圖片
public static Stream GetPhoto(PhotoSize size)
{
string path = HttpContext.Current.Server.MapPath("~/Images/");
switch (size)
{
case PhotoSize.Small:
path += "placeholder-100.jpg";
break;
case PhotoSize.Medium:
path += "placeholder-200.jpg";
break;
case PhotoSize.Large:
path += "placeholder-600.jpg";
break;
default:
path += "placeholder-600.jpg";
break;
}
return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
}
//獲取最新上傳的一張圖片,顯示在主頁中
public static Stream GetFirstPhoto(int albumid, PhotoSize size)
{
using (SqlConnection connection = new SqlConnection(ConnString))
{
using (SqlCommand command = new SqlCommand("GetFirstPhoto", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@AlbumID", albumid));
command.Parameters.Add(new SqlParameter("@Size", (int)size));
bool filter = !(HttpContext.Current.User.IsInRole("FrIEnds") || HttpContext.Current.User.IsInRole("Administrators"));
command.Parameters.Add(new SqlParameter("@IsPublic", filter));
connection.Open();
object result = command.ExecuteScalar();
try
{
return new MemoryStream((byte[])result);
}
catch
{
return null;
}
}
}
}
//此方法用於獲取一個相冊裡(根據AlbumID指定)的所有圖片
public static List<Photo> GetPhotos(int AlbumID)
{
using (SqlConnection connection = new SqlConnection(ConnString))
{
using (SqlCommand command = new SqlCommand("GetPhotos", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@AlbumID", AlbumID));
bool filter = !(HttpContext.Current.User.IsInRole("FrIEnds") || HttpContext.Current.User.IsInRole("Administrators"));
command.Parameters.Add(new SqlParameter("@IsPublic", filter)); //是否公開
connection.Open();
List<Photo> list = new List<Photo>();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Photo temp = new Photo(
(int)reader["PhotoID"],
(int)reader["AlbumID"],
(string)reader["Caption"]);
list.Add(temp);
}
}
return list;
}
}
}
//隨機指定一個相冊中的圖片
public static List<Photo> GetPhotos()
{
return GetPhotos(GetRandomAlbumID());
}
//添加圖片(指定相冊號,標題,以及原始圖片
public static void AddPhoto(int AlbumID, string Caption, byte[] BytesOriginal)
{
using (SqlConnection connection = new SqlConnection(ConnString))
{
using (SqlCommand command = new SqlCommand("AddPhoto", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@AlbumID", AlbumID));
command.Parameters.Add(new SqlParameter("@Caption", Caption));
command.Parameters.Add(new SqlParameter("@BytesOriginal", BytesOriginal));
command.Parameters.Add(new SqlParameter("@BytesFull", ResizeImageFile(BytesOriginal, 600)));
command.Parameters.Add(new SqlParameter("@BytesPoster", ResizeImageFile(BytesOriginal, 198)));
command.Parameters.Add(new SqlParameter("@BytesThumb", ResizeImageFile(BytesOriginal, 100)));
connection.Open();
command.ExecuteNonQuery();
}
}
}
//移除一張圖片(指定圖片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();
}
}
}
}
private static Size CalculateDimensions(Size oldSize, int targetSize)
{
Size newSize = new Size();
if (oldSize.Height > oldSize.Width)
{
newSize.Width = (int)(oldSize.Width * ((float)targetSize / (float)oldSize.Height));
newSize.Height = targetSize;
}
else
{
newSize.Width = targetSize;
newSize.Height = (int)(oldSize.Height * ((float)targetSize / (float)oldSize.Width));
}
return newSize;
}
//批量上傳時給出upload文件夾中的所有圖片,以列表的形式顯示
public static ICollection ListUploadDirectory()
{
DirectoryInfo d = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath("~/Upload"));
return d.GetFileSystemInfos("*.jpg");
}
}