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");
}
}