直接显示缩略图

原本想将所有照片生成缩略图之后再在系统上显示缩略图的,却在批量生成缩略图的时候提示了内存不足的情况,找了很久都没找到好方法,所以就找到了以下的解决方法!如果大家有好的批量生成缩略图的方法请赐教!


具体原理是将照片读取,然后直接输出缩略图


<%@ WebHandler Language="C#" Class="GetSmallPhoto" %>

using System;
using System.Web;
using System.Drawing; 
using System.IO;

public class GetSmallPhoto : IHttpHandler
{

    //图片所在文件夹 
    static string picturesPath = @"F:\PIC\";
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "image/jpeg";
        //获取到传递过来的img字符串,比如 
        string URL = context.Request.QueryString["URL"];
        string img = URL.Replace(System.Configuration.ConfigurationManager.AppSettings["WebURL"] + "/", "");//@"2014\ceshi\p2\w2\10212702\竞品堆头端架\IMG_2557.JPG"; //context.Request.Params["img"];
        string path = picturesPath + img;
        //如果文件存在才会去读取,减少使用try,catch,提高程序性能 
        if (File.Exists(path))
        {
            //载入这个图片 
            Image big = Image.FromFile(path);
            //如果可以获取到文件,才会执行下面的代码 
            if (big != null)
            {
                //设定最大的宽度,可以修改来生成更小的缩略图 
                int newWidth = 100;
                //根据图片的宽高比来生成一个位图 
                Bitmap bitmap = new Bitmap(newWidth, newWidth * big.Height / big.Width);
                //根据图板来创建一个图画 
                Graphics g = Graphics.FromImage(bitmap);
                using (g)
                {
                    //将大图big画到自己定义的小图中bitmap 
                    g.DrawImage(big, 0, 0, bitmap.Width, bitmap.Height);
                    //直接将处理好的位图保存到响应输出流中,格式为jpeg! 
                    bitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
            }
        }
        else
        {
            //否则就发送一个文件不存在的信息到浏览器 
            context.Response.ContentType = "text/html";
            context.Response.Write("文件不存在");
            //或者发送一个文件不存在的图片 
            //context.Response.WriteFile("todo此处修改为图片所在路径"); 
        }
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

调用如下:

<ext:DataView runat="server" ID="DataView_MenDian" ItemSelector="div.thumb-wrap" EmptyText="没有数据!" OverItemCls="x-view-over">
                        <Store>
                            <ext:Store ID="Store1" runat="server" PageSize="10">
                                <Proxy>
                                    <ext:PageProxy DirectFn="App.direct.BindData" />
                                </Proxy>
                                <Model>
                                    <ext:Model ID="Model1" runat="server">
                                        <Fields>
                                            <ext:ModelField Name="MID" />
                                            <ext:ModelField Name="QiShu" />
                                            <ext:ModelField Name="ZhouShu" />
                                            <ext:ModelField Name="DaQu" />
                                            <ext:ModelField Name="KeHuLeiXing" />
                                            <ext:ModelField Name="ChenLieLeiXing" />
                                            <ext:ModelField Name="Name" />
                                            <ext:ModelField Name="URL" />
                                            <ext:ModelField Name="SmallURL" />
                                        </Fields>
                                    </ext:Model>
                                </Model>
                                <Sorters>
                                    <ext:DataSorter Property="MID" Direction="ASC" />
                                </Sorters>
                            </ext:Store>
                        </Store>
                        <Tpl ID="Tpl1" runat="server">
                            <Html>
						        <tpl for=".">
							        <div class="thumb-wrap" οnclick="PhotoClick('{URL}')">
								        <div class="thumb"><img src="GetSmallPhoto.ashx?URL={URL}" title="店号:{MID}"></div>
                                        <span>期数:{QiShu}  周数:{ZhouShu}</span>
                                        <span>大区:{DaQu}</span>
                                        <span>客户:{KeHuLeiXing}</span>
                                        <span>陈列:{ChenLieLeiXing}</span>
							        </div>
						        </tpl>
						        <div class="x-clear"></div>
					        </Html>
                        </Tpl>
                    </ext:DataView>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值