程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#清晰的圖片縮略方案

C#清晰的圖片縮略方案

編輯:C#入門知識

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

購吧網目前擁有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;
   &n

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved