使用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(00, bitmap.Width, bitmap.Height),
                    
new Rectangle(00, 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的速度快)

 

 

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;
        }
    }

}

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章