最精简的相册管理代码

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

}

时间: 2024-08-06 23:15:38

最精简的相册管理代码的相关文章

ASP.NET MVC4使用MongoDB制作相册管理_实用技巧

ASP.NET MVC4使用MongoDB制作相册管理实例分享 TIPS:1.Image转成Base64保存到mongodb字段          2.数据模型是嵌套的关联  首先定义Model层:  public class Photo : IEquatable<Photo> { [Required] public string PhotoName { get; set; } [Required] public string PhotoDescription { get; set; } pu

javaWEB实现相册管理的简单功能_java

这仅仅只是一个小小的相册管理,主要实现的功能:能够实现对图片的上传,统一浏览,单个下载,单个删除,只能删除自己上传的文件.现在对每个功能进行单个的解释:图片的上传  图片的上传在之前的文章中写的很清楚了,点击打开链接:<JavaEE实现前后台交互的文件上传与下载> . 在这个相册管理中,就不是单一的文件传了,还需要涉及到很多参数供其他功能模块的使用 <span style="font-size:24px;">//上传文件一般采用外面的 apache的上传工具包

十款免费QQ空间个性相册边框代码

&http://www.aliyun.com/zixun/aggregation/37954.html">nbsp;     当我们把照片上传到QQ空间里的时候,有没有想过修改一下相册的边框,使其看上去更加的可爱一些呢?如果你有考虑过这样的问题,但又不知道怎么下手,不防看看本文给你带去的一些好东西! 给大家介绍目前可用的十款QQ空间相册边框代码,希望大家能喜欢! 第一款:真的很想你 代码: javascript:window.top.space_addItem(16,21912,0

javascript-JS求一个查看简单相册的代码

问题描述 JS求一个查看简单相册的代码 具体就是跟QQ空间的相册有点相似 下面都是小的照片 点击其中一个 上面出现大的照片 求具体代码 解决方案 再页面上加一个div隐藏域 这个隐藏域就是那个点击出来的大图的样子 在那个小的图片上面加个链接 调用一下js方法 把点击的图片id或者什么唯一标识带到js方法里面 在js方法里面将那个隐藏的div显示 并且把div中的图片路径换成点击的那个图片 改变一下图片大小就好了 解决方案二: 其实就2张图片,小图放底下,大图浮动在小图上面,就有这种渐显效果了 看

用户权限管理代码

用户权限管理代码 define('PATTERN_MODE_INT',                1); define('PATTERN_MODE_STRING',        2); function bitToArray($data, $ruleArr, $mode = PATTERN_MODE_INT) {         $varData = array();         if($mode == PATTERN_MODE_INT)         {              

Android打开相机和相册实例代码_Android

本文实例为大家分享了Android打开相机和相册具体代码,供大家参考,具体内容如下 打开相机  /** * 选择相机 */ private void showCamera() { // 跳转到系统照相机 Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (cameraIntent.resolveActivity(getPackageManager()) != null) { // 设置系统相机拍照后的输出路

使用git建立本地仓储管理代码【转】

转自:http://blog.csdn.net/airk000/article/details/7738231 Git是Linus大神的代码管理工具,简直说是开发者的超级福音,而作为屌丝的个人开发者,自己购买服务器或者github付费服务显然不合算,那么能不能在本地建立一个git仓储来管理自己的代码呢?答案是当然可以! (安装git什么的就不说了,很简单的apt-get install git-core就OK) 本例中仓储设定在~/git_store/,而开发的代码所在文件夹为~/git_exa

Android打开相机和相册实例代码

本文实例为大家分享了Android打开相机和相册具体代码,供大家参考,具体内容如下 打开相机 /** * 选择相机 */ private void showCamera() { // 跳转到系统照相机 Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (cameraIntent.resolveActivity(getPackageManager()) != null) { // 设置系统相机拍照后的输出路径

相册管理软件《Clovor Photos》限免中

手机用的时间一长,相册里的照片就多到爆,特别是像小编这种没事喜欢掏出来拍两张的人,现在照片已经逼近两千了,随便翻一下照片都会觉得有点杂乱无章,不妨用用这款<Clovor Photos>工具,把重点的照片突出出来.<Clovor Photos>应用截图下载完软件之后,允许它访问相册,相册中的所有照片 就会以时间流的方式排列出来了,最晚的排在最前面,和自带相册正好相反.如果觉得哪张最好看,比如抓住了你美腻的一面,大图查看该图片时点击四叶草标志,之后再浏览的时候该照片就会自动放大.经过编