Asp.net相册管理代码

来源:互联网 发布:复旦cpu卡密码算法 编辑:程序博客网 时间:2024/06/02 07:03
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using System.Drawing;
  8. using System.Drawing.Drawing2D;
  9. using System.Drawing.Imaging;
  10. using System.IO;
  11. using System.Web;
  12. public class PhotoManager
  13. {
  14.     // 指定数据库连接串
  15.     public static string ConnString = "server=****;database=****; uid=sa; pwd=****; pooling=true";
  16.     //读取数据库中的照片
  17.     public static Stream GetPhoto(int photoid, PhotoSize size)
  18.     {
  19.         using (SqlConnection connection = new SqlConnection(ConnString))
  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.                 bool filter =
  27. !(HttpContext.Current.User.IsInRole("Friends") ||
  28. HttpContext.Current.User.IsInRole("Administrators"));
  29.                 command.Parameters.Add(new SqlParameter("@IsPublic", filter));
  30.                 connection.Open();
  31.                 object result = command.ExecuteScalar();
  32.                 try
  33.                 {
  34.                     return new MemoryStream((byte[])result);
  35.                 }
  36.                 catch
  37.                 {
  38.                     return null;
  39.                 }
  40.             }
  41.         }
  42.     }
  43.     //获取默认图片
  44.     public static Stream GetPhoto(PhotoSize size)
  45.     {
  46.         string path = HttpContext.Current.Server.MapPath("~/Images/");
  47.         switch (size)
  48.         {
  49.             case PhotoSize.Small:
  50.                 path += "placeholder-100.jpg";
  51.                 break;
  52.             case PhotoSize.Medium:
  53.                 path += "placeholder-200.jpg";
  54.                 break;
  55.             case PhotoSize.Large:
  56.                 path += "placeholder-600.jpg";
  57.                 break;
  58.             default:
  59.                 path += "placeholder-600.jpg";
  60.                 break;
  61.         }
  62.         return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
  63.     }
  64.     //获取最新上传的一张图片,显示在主页中
  65.     public static Stream GetFirstPhoto(int albumid, PhotoSize size)
  66.     {
  67.         using (SqlConnection connection = new SqlConnection(ConnString))
  68.         {
  69.             using (SqlCommand command = new SqlCommand("GetFirstPhoto", connection))
  70.             {
  71.                 command.CommandType = CommandType.StoredProcedure;
  72.                 command.Parameters.Add(new SqlParameter("@AlbumID", albumid));
  73.                 command.Parameters.Add(new SqlParameter("@Size", (int)size));
  74.                 bool filter =
  75. !(HttpContext.Current.User.IsInRole("Friends") ||
  76. HttpContext.Current.User.IsInRole("Administrators"));
  77.                 command.Parameters.Add(new SqlParameter("@IsPublic", filter));
  78.                 connection.Open();
  79.                 object result = command.ExecuteScalar();
  80.                 try
  81.                 {
  82.                     return new MemoryStream((byte[])result);
  83.                 }
  84.                 catch
  85.                 {
  86.                     return null;
  87.                 }
  88.             }
  89.         }
  90.     }
  91.     //此方法用于获取一个相册里(根据AlbumID指定)的所有图片
  92.     public static List<Photo> GetPhotos(int AlbumID)
  93.     {
  94.         using (SqlConnection connection = new SqlConnection(ConnString))
  95.         {
  96.             using (SqlCommand command = new SqlCommand("GetPhotos", connection))
  97.             {
  98.                 command.CommandType = CommandType.StoredProcedure;
  99.                 command.Parameters.Add(new SqlParameter("@AlbumID", AlbumID));
  100.                 bool filter =
  101. !(HttpContext.Current.User.IsInRole("Friends") ||
  102. HttpContext.Current.User.IsInRole("Administrators"));
  103.                 command.Parameters.Add(new SqlParameter("@IsPublic", filter));  //是否公开
  104.                 connection.Open();
  105.                 List<Photo> list = new List<Photo>();
  106.                 using (SqlDataReader reader = command.ExecuteReader())
  107.                 {
  108.                     while (reader.Read())
  109.                     {
  110.                         Photo temp = new Photo(
  111.                             (int)reader["PhotoID"],
  112.                             (int)reader["AlbumID"],
  113.                             (string)reader["Caption"]);
  114.                         list.Add(temp);
  115.                     }
  116.                 }
  117.                 return list;
  118.             }
  119.         }
  120.     }
  121.     //随机指定一个相册中的图片
  122.     public static List<Photo> GetPhotos()
  123.     {
  124.         return GetPhotos(GetRandomAlbumID());
  125.     }
  126.     //添加图片(指定相册号,标题,以及原始图片
  127.     public static void AddPhoto(int AlbumID, string Caption, byte[] BytesOriginal)
  128.     {
  129.         using (SqlConnection connection = new SqlConnection(ConnString))
  130.         {
  131.             using (SqlCommand command = new SqlCommand("AddPhoto", connection))
  132.             {
  133.                 command.CommandType = CommandType.StoredProcedure;
  134.                 command.Parameters.Add(new SqlParameter("@AlbumID", AlbumID));
  135.                 command.Parameters.Add(new SqlParameter("@Caption", Caption));
  136.                 command.Parameters.Add(new SqlParameter("@BytesOriginal", BytesOriginal));
  137.                 command.Parameters.Add(new SqlParameter("@BytesFull", ResizeImageFile(BytesOriginal, 600)));
  138.                 command.Parameters.Add(new SqlParameter("@BytesPoster", ResizeImageFile(BytesOriginal, 198)));
  139.                 command.Parameters.Add(new SqlParameter("@BytesThumb", ResizeImageFile(BytesOriginal, 100)));
  140.                 connection.Open();
  141.                 command.ExecuteNonQuery();
  142.             }
  143.         }
  144.     }
  145.     //移除一张图片(指定图片ID)
  146.     public static void RemovePhoto(int PhotoID)
  147.     {
  148.         using (SqlConnection connection = new SqlConnection(ConnString))
  149.         {
  150.             using (SqlCommand command = new SqlCommand("RemovePhoto", connection))
  151.             {
  152.                 command.CommandType = CommandType.StoredProcedure;
  153.                 command.Parameters.Add(new SqlParameter("@PhotoID", PhotoID));
  154.                 connection.Open();
  155.                 command.ExecuteNonQuery();
  156.             }
  157.         }
  158.     }
  159.     //编辑图片(编辑后的标题与所编辑的图片ID)
  160.     public static void EditPhoto(string Caption, int PhotoID)
  161.     {
  162.         using (SqlConnection connection = new SqlConnection(ConnString))
  163.         {
  164.             using (SqlCommand command = new SqlCommand("EditPhoto", connection))
  165.             {
  166.                 command.CommandType = CommandType.StoredProcedure;
  167.                 command.Parameters.Add(new SqlParameter("@Caption", Caption));
  168.                 command.Parameters.Add(new SqlParameter("@PhotoID", PhotoID));
  169.                 connection.Open();
  170.                 command.ExecuteNonQuery();
  171.             }
  172.         }
  173.     }
  174.     // 获取相册(在相册页面中调用)
  175.     public static List<Album> GetAlbums()
  176.     {
  177.         using (SqlConnection connection = new SqlConnection(ConnString))
  178.         {
  179.             using (SqlCommand command = new SqlCommand("GetAlbums", connection))
  180.             {
  181.                 command.CommandType = CommandType.StoredProcedure;
  182.                 bool filter =
  183. !(HttpContext.Current.User.IsInRole("Friends") ||
  184. HttpContext.Current.User.IsInRole("Administrators"));
  185.                 command.Parameters.Add(new SqlParameter("@IsPublic", filter));
  186.                 connection.Open();
  187.                 List<Album> list = new List<Album>();
  188.                 using (SqlDataReader reader = command.ExecuteReader())
  189.                 {
  190.                     while (reader.Read())
  191.                     {
  192.                         Album temp = new Album(
  193.                             (int)reader["AlbumID"],
  194.                             (int)reader["NumberOfPhotos"],
  195.                             (string)reader["Caption"],
  196.                             (bool)reader["IsPublic"]);
  197.                         list.Add(temp);
  198.                     }
  199.                 }
  200.                 return list;
  201.             }
  202.         }
  203.     }
  204.     //添加新相册(相册名与是否公开属性)
  205.     public static void AddAlbum(string Caption, bool IsPublic)
  206.     {
  207.         using (SqlConnection connection = new SqlConnection(ConnString))
  208.         {
  209.             using (SqlCommand command = new SqlCommand("AddAlbum", connection))
  210.             {
  211.                 command.CommandType = CommandType.StoredProcedure;
  212.                 command.Parameters.Add(new SqlParameter("@Caption", Caption));
  213.                 command.Parameters.Add(new SqlParameter("@IsPublic", IsPublic));
  214.                 connection.Open();
  215.                 command.ExecuteNonQuery();
  216.             }
  217.         }
  218.     }
  219.     //删除一个相册(删除一个相册时里面的所有照片也同时删除)
  220.     public static void RemoveAlbum(int AlbumID)
  221.     {
  222.         using (SqlConnection connection = new SqlConnection(ConnString))
  223.         {
  224.             using (SqlCommand command = new SqlCommand("RemoveAlbum", connection))
  225.             {
  226.                 command.CommandType = CommandType.StoredProcedure;
  227.                 command.Parameters.Add(new SqlParameter("@AlbumID", AlbumID));
  228.                 connection.Open();
  229.                 command.ExecuteNonQuery();
  230.             }
  231.         }
  232.     }
  233.     //编辑相册(编辑标量与公开属性,指定相册ID号)
  234.     public static void EditAlbum(string Caption, bool IsPublic, int AlbumID)
  235.     {
  236.         using (SqlConnection connection = new SqlConnection(ConnString))
  237.         {
  238.             using (SqlCommand command = new SqlCommand("EditAlbum", connection))
  239.             {
  240.                 command.CommandType = CommandType.StoredProcedure;
  241.                 command.Parameters.Add(new SqlParameter("@Caption", Caption));
  242.                 command.Parameters.Add(new SqlParameter("@IsPublic", IsPublic));
  243.                 command.Parameters.Add(new SqlParameter("@AlbumID", AlbumID));
  244.                 connection.Open();
  245.                 command.ExecuteNonQuery();
  246.             }
  247.         }
  248.     }
  249.     //获取随机相册号(1-最大相册号之间)
  250.     public static int GetRandomAlbumID()
  251.     {
  252.         using (SqlConnection connection = new SqlConnection(ConnString))
  253.         {
  254.             using (SqlCommand command = new SqlCommand("GetNonEmptyAlbums", connection))
  255.             {
  256.                 command.CommandType = CommandType.StoredProcedure;
  257.                 connection.Open();
  258.                 List<Album> list = new List<Album>();
  259.                 using (SqlDataReader reader = command.ExecuteReader())
  260.                 {
  261.                     while (reader.Read())
  262.                     {
  263.                         Album temp = new Album((int)reader["AlbumID"], 0, "", false);
  264.                         list.Add(temp);
  265.                     }
  266.                 }
  267.                 try
  268.                 {
  269.                     Random r = new Random();
  270.                     return list[r.Next(list.Count)].AlbumID;
  271.                 }
  272.                 catch
  273.                 {
  274.                     return -1;
  275.                 }
  276.             }
  277.         }
  278.     }
  279.     //指定图片大小,根据自定义而缩放图片
  280.     private static byte[] ResizeImageFile(byte[] imageFile, int targetSize)
  281.     {
  282.         using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
  283.         {
  284.             Size newSize = CalculateDimensions(oldImage.Size, targetSize);
  285.             using (Bitmap newnewImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb))
  286.             {
  287.                 using (Graphics canvas = Graphics.FromImage(newImage))
  288.                 {
  289.                     canvas.SmoothingMode = SmoothingMode.AntiAlias;
  290.                     canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
  291.                     canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
  292.                     canvas.DrawImage(oldImage, new Rectangle(new Point(0, 0), newSize));
  293.                     MemoryStream m = new MemoryStream();
  294.                     newImage.Save(m, ImageFormat.Jpeg);
  295.                     return m.GetBuffer();
  296.                 }
  297.             }
  298.         }
  299.     }
  300.     private static Size CalculateDimensions(Size oldSize, int targetSize)
  301.     {
  302.         Size newnewSize = new Size();
  303.         if (oldSize.Height > oldSize.Width)
  304.         {
  305.             newSize.Width = (int)(oldSize.Width * ((float)targetSize / (float)oldSize.Height));
  306.             newSize.Height = targetSize;
  307.         }
  308.         else
  309.         {
  310.             newSize.Width = targetSize;
  311.             newSize.Height = (int)(oldSize.Height * ((float)targetSize / (float)oldSize.Width));
  312.         }
  313.         return newSize;
  314.     }
  315.     //批量上传时给出upload文件夹中的所有图片,以列表的形式显示
  316.     public static ICollection ListUploadDirectory()
  317.     {
  318.         DirectoryInfo d = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath("~/Upload"));
  319.         return d.GetFileSystemInfos("*.jpg");
  320.     }
  321. }
 
原创粉丝点击