博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 读取数据库 Image 字段,输出缩略图以及原图
阅读量:6876 次
发布时间:2019-06-26

本文共 4677 字,大约阅读时间需要 15 分钟。

  最近项目中用到了照片展示,开始做的是直接排列显示原图,无奈图片多了就卡的不行了,尤其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
rep = new Repository
(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
rep = new Repository
(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); } } } ///
/// 输出缩略图 /// ///
二进制数组 ///
HttpContext context 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; } }}

 

posted on
2012-08-29 09:34 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/HaoGuo/archive/2012/08/29/Thumbnail.html

你可能感兴趣的文章
丢了翅膀,他仍是天使
查看>>
适配不同分辨率的Android手机的简单处理方法
查看>>
台湾域名总量近期动态:总量达7万个 曾出现负增长
查看>>
JAX-WS Provider和Dispatch
查看>>
Flask表单处理Flask-WTF
查看>>
双Y轴图表的是与非
查看>>
初做领导
查看>>
Thinkphp3.2 增删改查
查看>>
部署Samba文件共享服务
查看>>
mysql复制一个新的数据库
查看>>
matlab 控制(helpqq)
查看>>
复杂系统是如何崩溃的 - 翻译
查看>>
Linux基础笔记vi
查看>>
10-利用思维导图梳理JavaSE-Java 集合
查看>>
在线图片翻转、旋转工具
查看>>
jquery利用sort方法对json数据排序
查看>>
要复习内容
查看>>
【Qt笔记】使用流处理 XML
查看>>
指针的使用
查看>>
5-pandas基础运算
查看>>