最近項目中用到了照片展示,開始做的是直接排列顯示原圖,無奈圖片多了就卡的不行了,尤其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; } }}