清晰的图片缩略方案

网略上广泛流传的三线性插值算法(效果并不是很好),代码如下:
using System;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace Ants.Tools
{
   
    public class Image
    {

        public int Width { get; set; }

        public int Height { get; set; }      
   
        private Image() { }
        public Image(int width, int height)
        {        
            this.Width = width;
            this.Height = height;
        }
          
        public MemoryStream getHightThumb(Stream imgData, string Mode_HW_W_H_Cut)
        {
            MemoryStream result = new MemoryStream();
            System.Drawing.Image.GetThumbnailImageAbort myCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallBack);
            try
            {
                System.Drawing.Image originalImage = System.Drawing.Image.FromStream(imgData);

                int X, Y;
                X = Width;
                Y = Height;

                int towidth = X;
                int toheight = Y;

                int x = 0;
                int y = 0;
                int ow = originalImage.Width;
                int oh = originalImage.Height;

                switch (Mode_HW_W_H_Cut)
                {
                    case "HW": //指定高宽缩放(可能变形)
  break; case "W"://指定宽,高按比例 toheight = originalImage.Height * X / originalImage.Width;
break; case "H//指定高,宽按比例
                        towidth = originalImage.Width * Y / originalImage.Height;
                       
break; case "Cut": if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) { oh = originalImage.Height; ow = originalImage.Height * towidth / toheight; y = 0; x = (originalImage.Width - ow) / 2; } else { ow = originalImage.Width; oh = originalImage.Width * Y / towidth; x = 0; y = (originalImage.Height - oh) / 2; } break; default: break; } System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.Clear(System.Drawing.Color.Transparent); g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel); bitmap.Save(result, System.Drawing.Imaging.ImageFormat.Jpeg); } catch { //do something } return result; }
    }
}
此算法可以满足一般的网站的需求,但是作为一个电子商务网站,商品的图片的清晰度直接影响到消费都对此商品的购买欲望。
为了找到更好的方案,终于让我们找到了一个好的组件:MagickNet 这个组件是用C++写的,不过网络上已经有可用的C#调用版,文章的
后我也会给出这个DLL文件,值得一提的是这个组件是开源的。大家可以去研究下。
MagickNet 的功能很多,我这里就贴出一下他的缩略方法的用法(MagickNet 的资料在网上很难早)
using System;
using System.Collections.Generic;
using System.Text;

namespace KeChenDLL
{
    public class UploadFile
    {
        private string _path;//要处理的图片(原图)地址
        public UploadFile(string path)
        {
            this._path = path;
        }
        private UploadFile()
        {
           
        }
        public void ReSize(int width, int height,string SaveToPath)
        {
            MagickNet.Image img = new MagickNet.Image(_path);
            img.Quality = 100;

            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(_path);
            int x = bitmap.Width;
            int y = bitmap.Height;
            float rank = ((float) x)/y;
            if (x > y)
            {
                height =Convert.ToInt32(height / rank);
            }
            else
            {
                width =Convert.ToInt32(width * rank);
            }

            img.Resize(new System.Drawing.Size(width, height));
            img.Write(SaveToPath);
            img.Dispose();
        }
    }
}
希望这篇文章能帮到需要的人。
MagickNet组件地址
posted @ 2009-11-18 17:39  Ants  阅读(1887)  评论(4编辑  收藏  举报