C#清晰的圖片縮減方案[轉載]

來源:互聯網
上載者:User

C#清晰的圖片縮減方案_.net教程網
http://www.soaspx.com/dotnet/csharp/csharp_20091118_1677.html

 

希望這篇文章能給對於需要經常產生縮圖的朋友提供協助!

購吧網目前擁有4000餘種商品,在售商品超過2000萬,其中圖片量截至目前已有8G。
目前我們的方案是用單獨的檔案伺服器存放商品的圖片,通過file.365goba.com訪問。
檔案伺服器上架一個用於上傳圖片的WCF服務對上傳的圖片進行縮減並儲存。

購吧網前期的縮減演算法用的是網略上廣泛流傳的三線性插值演算法(效果並不是很好),代碼如下:

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

 

原創文字只代表本人某一時間內的觀點或結論,本人不對涉及到的任何代碼擔保。轉載請標明出處!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.