使用MemoryStream动态生成清晰度高的缩略图

一般都是用自带的那个类实现图片的缩略图,但小图还行,生成大点的图就很模糊了。

生成图片的ImageUtil.cs类

 

 

using  System;
using  System.Collections.Generic;
using  System.Web;
using  System.Drawing;
using  System.Drawing.Drawing2D;
using  System.Drawing.Imaging;
using  System.Net;
using  System.IO;

///   <summary>
///  ImageUtil 的摘要说明
///   </summary>
public   class  ImageUtil
{
    
///   <summary>
    
///  创建缩略图
    
///   </summary>
    
///   <param name="src"> 来源页面
    
///  可以是相对地址或者绝对地址
    
///   </param>
    
///   <param name="width"> 缩略图宽度 </param>
    
///   <param name="height"> 缩略图高度 </param>
    
///   <returns> 字节数组 </returns>
     public   static   byte [] MakeThumbnail( string  src,  double  width,  double  height)
    {
        Image image;

        
//  相对路径从本机直接读取
         if  (src.ToLower().IndexOf( " http:// " ==   - 1 )
        {
            src 
=  HttpContext.Current.Server.MapPath(src);
            image 
=  Image.FromFile(src,  true );
        }
        
else   //  绝对路径从 Http 读取
        {
            HttpWebRequest req 
=  (HttpWebRequest)WebRequest.Create(src);
            req.Method 
=   " GET " ;
            HttpWebResponse resp 
=  (HttpWebResponse)req.GetResponse();
            Stream receiveStream 
=  resp.GetResponseStream();
            image 
=  Image.FromStream(receiveStream);
            resp.Close();
            receiveStream.Close();
        }
        
double  newWidth, newHeight;
        
if  (image.Width  >  image.Height)
        {
            newWidth 
=  width;
            newHeight 
=  image.Height  *  (newWidth  /  image.Width);
        }
        
else
        {
            newHeight 
=  height;
            newWidth 
=  (newHeight  /  image.Height)  *  image.Width;
        }
        
if  (newWidth  >  width)
        {
            newWidth 
=  width;
        }
        
if  (newHeight  >  height)
        {
            newHeight 
=  height;
            newWidth 
=  image.Width  *  (newHeight  /  image.Height);
        }
        
// 取得图片大小
        Size size  =   new  Size(( int )newWidth, ( int )newHeight);
        
// 新建一个bmp图片
        Image bitmap  =   new  Bitmap(size.Width, size.Height);
        
// 新建一个画板
        Graphics g  =  Graphics.FromImage(bitmap);
        
// 设置高质量插值法
        g.InterpolationMode  =  InterpolationMode.High;
        
// 设置高质量,低速度呈现平滑程度
        g.SmoothingMode  =  SmoothingMode.HighQuality;
        
// 清空一下画布
        g.Clear(Color.White);
        
// 在指定位置画图
        g.DrawImage(image,  new  Rectangle( 0 0 , bitmap.Width, bitmap.Height),
                    
new  Rectangle( 0 0 , image.Width, image.Height),
                    GraphicsUnit.Pixel);
        
// 保存高清晰度的缩略图
        MemoryStream stream  =   new  MemoryStream();
        bitmap.Save(stream, ImageFormat.Jpeg);
        
byte [] buffer  =  stream.GetBuffer();
        g.Dispose();
        image.Dispose();
        bitmap.Dispose();
        
return  buffer;
    }
}

 

 

 

使用Thumbnail.ashx做为调用的文件地址(ashx处理的比aspx的速度快)

 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
<%@ WebHandler Language="C#" Class="Thumbnail" %>

using System;
using System.Web;
/// <summary>
/// 生产缩略图 调用方式<img src="Thumbnail.ashx?width=200&height=150&src=2.JPG" />
/// </summary>
public class Thumbnail : IHttpHandler
{

    
public void ProcessRequest(HttpContext context)
    {
        
string src = GetQueryStringSrc(context);
        
double width = GetQueryStringWidth(context);
        
double height = GetQueryStringHeight(context);
        context.Response.ContentType 
= "image/jpeg";
        context.Response.Clear();
        
if (src.Length > 0 && width > 0 && height > 0)
        {
            
try
            {
                
byte[] buffer = ImageUtil.MakeThumbnail(src, width, height);
                context.Response.BinaryWrite(buffer);
                context.Response.Flush();
            }
            
catch (Exception exce)
            {
                
string errstr = exce.Message;
            }
        }
    }

    
#region handle query string

    
private string GetQueryStringSrc(HttpContext context)
    {
        
string src = context.Request.QueryString["src"];
        src 
= (src == null? "" : src;
        
return src;
    }

    
private double GetQueryStringWidth(HttpContext context)
    {
        
string sWidth = context.Request.QueryString["width"];
        sWidth 
= (sWidth == null? "" : sWidth;
        
double width = 0;
        
try
        {
            width 
= double.Parse(sWidth);
        }
        
catch
        {
        }
        
return width;
    }

    
private double GetQueryStringHeight(HttpContext context)
    {
        
string sHeight = context.Request.QueryString["height"];
        sHeight 
= (sHeight == null? "" : sHeight;
        
double height = 0;
        
try
        {
            height 
= double.Parse(sHeight);
        }
        
catch
        {
        }
        
return height;
    }

    
#endregion
    
public bool IsReusable
    {
        
get
        {
            
return false;
        }
    }

}

 

 

 

 

转载于:https://www.cnblogs.com/zmxmiss/archive/2009/08/24/1552943.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值