1.数据库设计
字段 类型
PhotoID int
AlbumID int
Caption nvarchar(50)
BytesOriginal image
BytesFull image
BytesPoster image
BytesThumb image
2.前台显示图片
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<img src="Handler.ashx?AlbumID=<%# Eval("AlbumID") %>&Size=M" class="photo_198" style="border:4px solid white" alt='示例照片,相册编号 <%
# Eval("AlbumID") %>' />
3.Handler.ashx
<%@ WebHandler Language="C#" Class="Handler" %>
![](/Images/OutliningIndicators/None.gif)
using System;
using System.IO;
using System.Web;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class Handler : IHttpHandler
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public bool IsReusable
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{
return true;
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void ProcessRequest (HttpContext context)
{
// 设置响应设置
context.Response.ContentType = "image/jpeg";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.BufferOutput = false;
// 设置 Size 参数
PhotoSize size;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
switch (context.Request.QueryString["Size"])
{
case "S":
size = PhotoSize.Small;
break;
case "M":
size = PhotoSize.Medium;
break;
case "L":
size = PhotoSize.Large;
break;
default:
size = PhotoSize.Original;
break;
}
// 设置 PhotoID 参数
Int32 id = -1;
Stream stream = null;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (context.Request.QueryString["PhotoID"] != null && context.Request.QueryString["PhotoID"] != "")
{
id = Convert.ToInt32(context.Request.QueryString["PhotoID"]);
stream = PhotoManager.GetPhoto(id, size);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} else
{
id = Convert.ToInt32(context.Request.QueryString["AlbumID"]);
stream = PhotoManager.GetFirstPhoto(id, size);
}
// 从数据库获取照片,如果未返回照片,将获取默认的“placeholder”照片
if (stream == null) stream = PhotoManager.GetPhoto(size);
// 将图像流写入响应流中
const int buffersize = 1024 * 16;
byte[] buffer = new byte[buffersize];
int count = stream.Read(buffer, 0, buffersize);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while (count > 0)
{
context.Response.OutputStream.Write(buffer, 0, count);
count = stream.Read(buffer, 0, buffersize);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
}
4.业务层类(PhotoManager 部分内容)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public static List<Photo> GetPhotos(int AlbumID)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString))
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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>();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
using (SqlDataReader reader = command.ExecuteReader())
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while (reader.Read())
{
Photo temp = new Photo(
(int)reader["PhotoID"],
(int)reader["AlbumID"],
(string)reader["Caption"]);
list.Add(temp);
}
}
return list;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
}
// Helper 函数
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
private static byte[] ResizeImageFile(byte[] imageFile, int targetSize)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
{
Size newSize = CalculateDimensions(oldImage.Size, targetSize);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb))
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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();
}
}
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
private static Size CalculateDimensions(Size oldSize, int targetSize)
{
Size newSize = new Size();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (oldSize.Height > oldSize.Width)
{
newSize.Width = (int)(oldSize.Width * ((float)targetSize / (float)oldSize.Height));
newSize.Height = targetSize;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} else
{
newSize.Width = targetSize;
newSize.Height = (int)(oldSize.Height * ((float)targetSize / (float)oldSize.Width));
}
return newSize;
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public static ICollection ListUploadDirectory()
{
DirectoryInfo d = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath("~/Upload"));
return d.GetFileSystemInfos("*.jpg");
}
![](/Images/OutliningIndicators/None.gif)
}
5.批量上传操作
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
protected void Button1_Click(object sender, ImageClickEventArgs e)
{
DirectoryInfo d = new DirectoryInfo(Server.MapPath("~/Upload"));
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
foreach (FileInfo f in d.GetFiles("*.jpg"))
{
byte[] buffer = new byte[f.OpenRead().Length];
f.OpenRead().Read(buffer, 0, (int)f.OpenRead().Length);
PhotoManager.AddPhoto(Convert.ToInt32(Request.QueryString["AlbumID"]), f.Name, buffer);
}
GridView1.DataBind();
}
6.上传控件
.net2.0 专门有一个上传服务器控件
<asp:FileUpload ID="PhotoFile" Runat="server" Width="416" FileBytes='<%# Bind("BytesOriginal") %>' CssClass="textfield" /><br />