C# 讀取資料庫 Image 欄位,輸出縮圖以及原圖

來源:互聯網
上載者:User

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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.