最近项目中用到了照片展示,开始做的是直接排列显示原图,无奈图片多了就卡的不行了,尤其Chrome,滚动条都动不了,只能改动了。。
代码保存在这里,算是备忘,比较简单,就不加说明了。
缩略图
Show Thumbnail
<%@ WebHandler Language="C#" Class="ShowThumbnail" %> using System; using System.Web; using Drision.Framework.Entity; using System.IO; using System.Drawing; using Drision.Framework.Repository.EF; using Drision.Framework.Web; using Drision.Framework.Repository; public class ShowThumbnail : IHttpHandler { public void ProcessRequest(HttpContext context) { int id = Convert.ToInt32(context.Request.QueryString["id"]); if (id != null) { using (DrisionDbContext cont = new DrisionDbContext(GlobalObject.ConnString)) { Repository<T_Attachment> rep = new Repository<T_Attachment>(cont); T_Attachment Attachment = rep.FindById(id); byte[] AttachData = Attachment.FileData; OutPutThumbnail(AttachData, context); } } //context.Response.ContentType = "text/plain"; //context.Response.Write("Hello World"); } //输出缩略图 public void OutPutThumbnail(byte[] AttachData,HttpContext context) { try { //写入内存流 using (MemoryStream stream = new MemoryStream(AttachData)) { using (Bitmap bm = new Bitmap(stream)) { //Bitmap bm = null; Image image = null; //bm = new Bitmap(stream); int width = 100; int height = (int)(width * ((double)bm.Height / (double)bm.Width)); // getthumbnailimage生成缩略图 image = bm.GetThumbnailImage(width, height, null, IntPtr.Zero); context.Response.ContentType = "image/jpeg"; image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); image.Dispose(); } } } catch (Exception ex) { context.Response.ContentType = "text/plain"; context.Response.Write(ex.Message); } } public bool IsReusable { get { return false; } } }
原图
Show Source Image
<%@ WebHandler Language="C#" Class="ShowSourceImage" %> using System; using System.Web; using Drision.Framework.Entity; using System.IO; using System.Drawing; using Drision.Framework.Repository.EF; using Drision.Framework.Web; using Drision.Framework.Repository; public class ShowSourceImage : IHttpHandler { public void ProcessRequest(HttpContext context) { int id = Convert.ToInt32(context.Request.QueryString["id"]); if (id != null) { using (DrisionDbContext cont = new DrisionDbContext(GlobalObject.ConnString)) { Repository<T_Attachment> rep = new Repository<T_Attachment>(cont); T_Attachment Attachment = rep.FindById(id); byte[] AttachData = Attachment.FileData; //OutPutThumbnail(AttachData, context); //context.Response.ContentType = "text/plain"; //context.Response.Write("Hello World"); context.Response.ContentType = "image/jpeg"; context.Response.BinaryWrite(AttachData); } } } /// <summary> /// 输出缩略图 /// </summary> /// <param name="AttachData">二进制数组</param> /// <param name="context">HttpContext context</param> public void OutPutThumbnail(byte[] AttachData, HttpContext context) { try { //写入内存流 using (MemoryStream stream = new MemoryStream(AttachData)) { using (Bitmap bm = new Bitmap(stream)) { //Bitmap bm = null; Image image = null; //bm = new Bitmap(stream); int width = 100; int height = (int)(width * ((double)bm.Height / (double)bm.Width)); // getthumbnailimage生成缩略图 image = bm.GetThumbnailImage(width, height, null, IntPtr.Zero); context.Response.ContentType = "image/jpeg"; image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); image.Dispose(); } } } catch (Exception ex) { context.Response.ContentType = "text/plain"; context.Response.Write(ex.Message); } } public bool IsReusable { get { return false; } } }